Lecture 14: Control Flow. The break, continue, goto statements.

Slides:



Advertisements
Similar presentations
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Advertisements

1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
Computer Science 1620 Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
CS 117 Spring 2002 Repetition Hanly Chapter 4 Friedman-Koffman Chapter 5.
CSC 200 Lecture 4 Matt Kayala 1/30/06. Learning Objectives Boolean Expressions –Building, Evaluating & Precedence Rules Branching Mechanisms –if-else.
1 CS 192 Lecture 9 Winter 2003 December 19, 2003 Dr. Shafay Shamail.
Objectives You should be able to describe:
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
CONTROLLING PROGRAM FLOW
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
Lecture 15: Control Flow (cont). 2 Lecture Contents: t Design and implementation of programs illustrating linear algorithms, branch algorithms and loop.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
Switch Statement Is a selection control structure for multi-way branching. SYNTAX switch ( IntegralExpression ) { case Constant1 : Statement(s); // optional.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Chapter 05 (Part III) Control Statements: Part II.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
Control Structures RepetitionorIterationorLooping Part I.
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.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: The while Statement cin within a while Loop The for.
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
Decision Making and Branching (cont.)
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter 5 Repetition. 2 Objectives You should be able to describe: The while Statement cin within a while Loop The for Statement The do Statement Common.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
1 Looping Chapter 6 2 Getting Looped in C++ Using flags to control a while statement Trapping for valid input Ending a loop with End Of File condition.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Computer Programming -1-
Review 1.
REPETITION CONTROL STRUCTURE
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Branching Constructs Review
Chapter 2.2 Control Structures (Iteration)
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
INC 161 , CPE 100 Computer Programming
Conditinoal Constructs Review
Introduction to Programming
Structured Program Development in C
Chapter 7 Additional Control Structures
Conditinoal Constructs Review
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Chapter 2.2 Control Structures (Iteration)
Lab5 PROGRAMMING 1 Loop chapter4.
Looping III (do … while statement)
Objectives You should be able to describe: The while Statement
Building Java Programs
CSC215 Lecture Control Flow.
Programming Fundamental
Presentation transcript:

Lecture 14: Control Flow. The break, continue, goto statements

2 Lecture Contents: t Structured Programming Basics t The break control statement t The continue control statement t The goto control statement t Nested loops t Bit wise operators t Shift left ( >) operators t Demo programs and Exercises

3 Structured Programming Basics Control structureC++ source statement Sequencefunction call statements, assignment statement = ; Selectionif, if … else, switch Repetitionfor, while, do … while

4 break, continue, goto stmts  The break control statement  Syntax: break; t Usage: –Element of case clause in switch statement –Unconditional loop exit

5 break, continue, goto stmts  The continue control statement  Syntax: continue; t Usage: –Skip the rest of the loop body without exiting the loop

6 break, continue example for (int i=1; i<=10; i++) { cout << "\nIteration Nr “ << i << “ – 1st part loop "; cout << “\nIteration Nr “ << i << “ – 2nd part loop \n"; }

7 break, continue example for (int i=1; i<=10; i++) { cout << "\nIteration Nr “ << i << “ – 1st part loop "; cout << “\nIteration Nr “ << i << “ – 2nd part loop \n"; if ( i==5 ) break; }

8 break, continue example for (int i=1; i<=10; i++) { cout << "\nIteration Nr “ << i << “ – 1st part loop"; if ( i==3 ) continue; cout << “\nIteration Nr “ << i << “ – 2nd part loop\n"; if ( i==5 ) break; }

9 break, continue, goto stmts  The goto control statement t Remark: Forget the goto statement. Structured programming is “go to-less” programming. t Syntax:goto ; t Usage: –Unconditional transfer of control to statement labeled

Nested Loops t As with if statements, loop statements can be nested t Each time outer loop is repeated, any inner loop is restarted - loop control components are reevaluated and all required iterations are performed

Nested Loops t Possible to nest loops –Loops inside other loops –Start with outer loop jump to inner loop –Inner loop continues until complete –Back to outer loop and start again t NestLoop.cpp examples

12 Nested Logic – variant 1 outer = 1; while(outer <= 3) { inner = 1; // Resets for each iteration of the outer loop while(inner <= 3) { cout << outer << " " << inner << endl; inner = inner + 1; } // End of the nested loop outer = outer + 1; // Increment the outer loop counter } // The end of the outer loop

13 Nested Logic – variant 1 Relation btw outer and inner counters Outer |1|2|3| |||| Inner | 1,2,3| 1,2,3| 1,2,3| Total number of iterations for the inner loop body is =3*3= = 9

14 Nested Logic – variant 2 outer = 1; while(outer <= 3) { inner = 1; // Resets for each iteration of the outer loop while(inner <= outer) { cout << outer << " " << inner << endl; inner = inner + 1; } // End of the nested loop outer = outer + 1; // Increment the outer loop counter } // The end of the outer loop

15 Nested Logic – variant 2 Relation btw outer and inner counters Outer |1|2|3| |||| Inner | 1| 1,2| 1,2,3| Total number of iterations for the inner loop body is = = 6

16 Nested Logic – variant 3 outer = 1; while(outer <= 3) { inner = 1; // Resets for each iteration of the outer loop while(inner < outer) { cout << outer << " " << inner << endl; inner = inner + 1; } // End of the nested loop outer = outer + 1; // Increment the outer loop counter } // The end of the outer loop

17 Nested Logic – variant 3 Relation btw outer and inner counters Outer |1|2|3| |||| Inner | ?| ?| ?| Total number of iterations for the inner loop body is = …

18 Listing 5.13 Nested for loop program

19 Listing 5.13 Nested for loop program (continued)

20 Listing 5.14 Displaying the multiplication table

21 Listing 5.14 Displaying the multiplication table (continued)

22 Nested loops (reminder) Example: for (i=1; i<=10; i++) for(j=1; j<=5; j++) cout << setw(8) << i*j;

23 Nested loops (reminder) Example: for (i=1; i<=10; i++) { for(j=1; j<=i; j++) cout << setw(8) << i*j; cout << endl; }

24 Bit wise operators int a, b; // logical And operator a = 22 && 22;cout << ‘\n’ << a; // bit wise And operator b = 22 & 22;cout << ‘\n’ << b;

25 Bit wise operators int a, b; // logical Or operator a = 22 || 22;cout << ‘\n’ << a; // bit wise Or operator b = 22 | 22;cout << ‘\n’ << b;

26 Bit wise operators int a, b; // NO logical EOr operator // bit wise EOr operator b = 22 ^ 22;cout << endl << b; a = 12 ^ 23;cout << endl << a;

27 Shift left ( >) operators int a=32, b=1024; // Shift left (<<) operator cout << ‘\n’ << a << “ “ << a << 2; cout << ‘\n’ << a << “ “ << (a << 2); // Shift right (>>) operator cout > 3);

28 Exercise 14.1 Build programs based on linear, branch and loop algorithms:  Write double powm(double x, int n); function to return x n (x raised to power of n); Create driver program to test powm(3.0, 0); powm(4.0, 2); powm(2.0, -2); Expected return value:

29 Exercise 14.2 Build programs based on linear, branch and loop algorithms: t Character counting program: in C++ (cin.get()) style in C (getchar()) style; Hint: See back page on handout for details

30 Exercise 14.3 Build programs based on linear, branch and loop algorithms: To display Amount saved entering Principal, Rate and Duration using the well known formulae Amount = Principal * (1 + Rate/100) Duration using run time function double pow(double x,double y) /#include or #include / or the user function double powm(double x, int n);

31 Exercise 14.4 Build programs based on linear, branch and loop algorithms: To generate a table of multiples of a given number. The user should enter the number and the program should display a table of ten columns and five lines

32 Before lecture end Lecture: Control Flow. The break, continue, goto statements More to read: Friedman/Koffman, Chapter 05

33 Thank You For Your Attention!