ECE 103 Engineering Programming Chapter 20 Change in Flow of Control

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 8 Statement-Level Control Structure. Introduction Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program statements.
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.
L EC. 03: C ONTROL STATEMENTS Fall Java Programming.
C Programming Lecture 12. The Compound Statement b A compound statement is a series of declarations and statements surrounded by braces. b A compound.
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
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)
CPS120: Introduction to Computer Science Decision Making in Programs.
Controlling Execution Dong Shao, Nanjing Unviersity.
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.
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.
 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
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
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.
Control Structures (Repetition structure) Jump Statements
Decision Making in C.
Fundamentals of PL/SQL part 2 (Basics)
Chapter 6: Loops.
Chapter 4 Repetition Statements (loops)
Def: A control structure is a control statement and
Unit-1 Introduction to Java
Decisions Chapter 4.
C Program Controls + Flow Structure
Week 4 - Monday CS222.
Programmazione I a.a. 2017/2018.
Flow of Control.
The Linux Command Line Chapter 29
INC 161 , CPE 100 Computer Programming
Flow of Control.
Control Structures In Text: Chapter 8.
Iteration: Beyond the Basic PERFORM
Flow of Control.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
ECE 103 Engineering Programming Chapter 32 Array Parameters
Relational, Logical, and Equality Operators
ECE 103 Engineering Programming Chapter 56 Runtime Errors
ECE 103 Engineering Programming Chapter 19 Nested Loops
ECE 103 Engineering Programming Chapter 12 More C Statements
ECE 103 Engineering Programming Chapter 51 Random Numbers
ECE 103 Engineering Programming Chapter 46 argc, argv, envp
ECE 103 Engineering Programming Chapter 37 C Macro Parameters
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
CS 106 Computing Fundamentals II Chapter 69 “Event Loop”
Program Flow.
ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1
More Loops Topics Counter-Controlled (Definite) Repetition
ECE 103 Engineering Programming Chapter 18 Iteration
More Loops Topics Counter-Controlled (Definite) Repetition
ECE 103 Engineering Programming Chapter 22 Selection
More Loops Topics Counter-Controlled (Definite) Repetition
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:

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 Wong @ PSU ECE

Syllabus Break Continue Return goto

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

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

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

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

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

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

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

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