Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 161 Introduction to Programming and Problem Solving Chapter 18 Control Flow Through C++ Program Herbert G. Mayer, PSU Status 10/8/2014 Initial content.

Similar presentations


Presentation on theme: "CS 161 Introduction to Programming and Problem Solving Chapter 18 Control Flow Through C++ Program Herbert G. Mayer, PSU Status 10/8/2014 Initial content."— Presentation transcript:

1 CS 161 Introduction to Programming and Problem Solving Chapter 18 Control Flow Through C++ Program Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim from ECE 103 material by prof. Phillip Wong @ PSU

2 Syllabus Types of Jumps Break Continue Return Goto

3 2 Types of Jumps Conditional 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: http://msdn.microsoft.com/en-us/library/7k66t42c.aspx

4 3 break 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 When encountered inside a function:  continue immediately transfers control to the conditional expression of the nearest enclosing loop statement in which it appears

5 4 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

6 5 goto When encountered inside a function:  goto immediately transfers control to the statement labeled by a specified identifier Syntax: goto label; … label: Statements …

7 6 Example: goto #include 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 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

8 7 Hey, goto seems like a neat way to jump around in a program. So, I can use it wherever I like now, right?

9 8 goto is a mostly outdated feature from the old days before structured programming techniques were adopted Practiced by programmers who previosuly wrote assembly code Java for example has no longer any goto statements! Avoid over-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

10 9 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.


Download ppt "CS 161 Introduction to Programming and Problem Solving Chapter 18 Control Flow Through C++ Program Herbert G. Mayer, PSU Status 10/8/2014 Initial content."

Similar presentations


Ads by Google