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.

Slides:



Advertisements
Similar presentations
Selection Feature of C: Decision making statements: It allow us to take decisions as to which code is to be executed next. Since these statements control.
Advertisements

Chapter 13 Control Structures in C. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Control Structures if Statement if-else Statement switch.
ECE 103 Engineering Programming Chapter 54 Recursion Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
Chapter 8 Statement-Level Control Structure. Introduction Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program statements.
Control Flow C and Data Structures Baojian Hua
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
1 MATERI PENDUKUNG JUMP Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
C Programming Lecture 12. The Compound Statement b A compound statement is a series of declarations and statements surrounded by braces. b A compound.
CS 161 Introduction to Programming and Problem Solving Chapter 13 C++ Preprocessor Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
CPS120 Introduction to Computer Science Iteration (Looping)
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Control Statements (Decision Making)
CPS120: Introduction to Computer Science Decision Making in Programs.
ISBN Chapter 8 Statement-Level Control Structures.
Controlling Execution Dong Shao, Nanjing Unviersity.
ECE 103 Engineering Programming Chapter 24 Sorting Herbert G. Mayer, PSU CS Status 6/2/2015 Initial content copied verbatim from ECE 103 material developed.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
CPS120 Introduction to Computer Science Iteration (Looping)
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
Control Flow Statements
CS 161 Introduction to Programming and Problem Solving Chapter 17 Nested Loops Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
Engineering Computing I Chapter 3 Control Flow. Chapter 3 - Control Flow The control-flow of a language specify the order in which computations are performed.
CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
Loop Control อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 8.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Program Control: Selection Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
Decision Making in C.
Fundamentals of PL/SQL part 2 (Basics)
Chapter 6: Loops.
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Def: A control structure is a control statement and
Unit-1 Introduction to Java
C Program Controls + Flow Structure
Programmazione I a.a. 2017/2018.
Flow of Control.
11/10/2018.
INC 161 , CPE 100 Computer Programming
FLOW OF CONTROL.
Lecture 2: Logical Problems with Choices
Flow of Control.
IF if (condition) { Process… }
Structured Program
Iteration: Beyond the Basic PERFORM
Flow of Control.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
ECE 103 Engineering Programming Chapter 19 Nested Loops
ECE 103 Engineering Programming Chapter 12 More C Statements
Program Flow.
ECE 103 Engineering Programming Chapter 20 Change in Flow of Control
More Loops Topics Counter-Controlled (Definite) Repetition
ECE 103 Engineering Programming Chapter 18 Iteration
More Loops Topics Counter-Controlled (Definite) Repetition
CSC215 Lecture Control Flow.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Programming Language  C Control Flow
Presentation transcript:

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 PSU

Syllabus Types of Jumps Break Continue Return Goto

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:

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

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

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

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

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

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

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.