Download presentation
Presentation is loading. Please wait.
Published byบุษราคัม สโตเกอร์ Modified over 5 years ago
1
ECE 103 Engineering Programming Chapter 20 Change in Flow of Control
Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed by Professor Phillip PSU ECE
2
Syllabus Break Continue Return goto
3
Types of Jumps Conditional Jumps Unconditional Jumps
A conditional expression is tested, which determines the execution sequence. Examples: if, switch, while, for Unconditional Jumps There is no conditional test. Execution jumps immediately to a specific location. Examples: break, continue, return, goto Definitions to follow are from the Microsoft MSDN C++ Reference Library: 2
4
break continue When encountered inside a function:
break immediately ends execution of the nearest enclosing loop in which it appears Control passes to the statement that follows the ended statement, if any; else to the implied return at the ending } continue continue immediately transfers control to the conditional expression of the nearest enclosing loop statement in which it appears 3
5
return When encountered inside a function:
return terminates execution of the function Control returns to the calling function Note that even the main() function is “called”, namely by the OS Execution resumes in the calling function at the point immediately following the associated call A true function returns a value to the place of call A return statement in the main() function will terminate execution of the entire program by returning to the OS 4
6
goto When encountered inside a function: Syntax: goto label; … label:
goto immediately transfers control to the statement labeled by a specified identifier Syntax: goto label; … label: Statements … 5
7
Example: goto #include <stdio.h> int main( void ) { // main
Outer loop executing. i = 0 Inner loop executing. j = 0 Inner loop executing. j = 1 Outer loop executing. i = 1 Inner loop executing. j = 0 Inner loop executing. j = 1 Outer loop executing. i = 2 Inner loop executing. j = 0 Inner loop executing. j = 1 Outer loop executing. i = 3 Inner loop executing. j = 0 Jumped to stop. i = 3 Example: goto #include <stdio.h> int main( void ) { // main int i, j; for( i = 0; i < 10; i++ ) { // magic # printf( "Outer loop executing. i = %d\n", i ); for( j = 0; j < 2; j++ ) { // also magic printf( " Inner loop executing. j = %d\n", j ); if( i == 3 ) goto stop; } //end for // This message does not print printf( "Loop exited. Impossible to reach i = %d\n", i ); stop: printf( "Jumped to stop. i = %d\n", i ); } //end main 6
8
Hey, goto seems like a neat way to jump around in a program
Hey, goto seems like a neat way to jump around in a program. So, I can use it wherever I like now, right? 7
9
Practiced by programmers who previously wrote assembly code
goto is a mostly outdated feature from the old days before structured programming techniques were adopted Practiced by programmers who previously wrote assembly code Java for example has no longer any goto statements! Avoid using gotos: Excessive use of goto makes it difficult to follow the logic of a program; creates “spaghetti code” Most code can be rewritten to accomplish the same thing using structured language features 8
10
Does this mean I can never use a goto?
If an unrecoverable error occurs inside a deeply nested loop, a goto could be used to jump directly out of the loops to an error handling routine for ... while ... for … do … if( not_recoverable_condition ) goto ErrorHandler; ErrorHandler: /* Process the error */ No. 9
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.