Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.

Slides:



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

While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Computer Science 1620 Loops.
Iterative Constructs – chapter 4 pp 189 to 216 This lecture covers the mechanisms for deciding the conditions to repeat action. Some materials are from.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
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.
Loops. COMP104 Loops / Slide 2 Shortcut Assignment * C++ has a set of operators for applying an operation to a variable and then storing the result back.
Chapter 5: Control Structures II (Repetition)
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Iterative Constructs Mechanisms for deciding under what conditions an action should be repeated JPC and JWD © 2002 McGraw-Hill, Inc.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
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.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
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.
1 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statement } while.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
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.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
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.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
01/05/100 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while.
REPETITION CONTROL STRUCTURE
Branching Constructs Review
Control Structures II (Repetition)
CiS 260: App Dev I Chapter 4: Control Structures II.
Programming Fundamentals
Conditinoal Constructs Review
For & do/while Loops.
Expression Review what is the result and type of these expressions?
Conditinoal Constructs Review
Repetition Control Structure
Chapter 6: Repetition Statements
Control Structures Part 1
Alternate Version of STARTING OUT WITH C++ 4th Edition
2.6 The if/else Selection Structure
Presentation transcript:

Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway if? How does multiway if relate to nested if? l what is a switch statement? is it better than multiway if? l what does break inside switch do? l what is conditional assignment? what construct can be used instead? l what are named constants? why are they needed? l what is a block? what is special about declaring a variable inside a block? what is a scope of a variable? l what is programming idiom? l what is a unary, binary, ternary operator? 1

Iterative Constructs while, for, do-while

Iterative Constructs l provide n ability to execute the same code multiple times l three constructs n while statement n do-while statement n for statement 3

The while Statement l syntax while ( expression ) action l semantics n if expression is true then execute action n repeat this process until expression evaluates to false l action is either a single statement or a block expression action truefalse 4

The do - while Statement l syntax do action while ( expression ); l semantics n execute action n if expression is true then execute action again n repeat this process until expression evaluates to false l action is either a single statement or a block action true false expression 5

The for Statement l syntax for( init_statement ; expression ; post_statement ) action l semantics n execute init_statement n evaluate expression if true –execute action –execute post_statement –repeat l example for (int i = 0; i < 20; ++i) cout << "i is " << i << endl; expression action true false init_statement post_statement 6

Iterate and Keep Track Idiom l what is idiom again? l often need to iterate while keep track of some value across iterations – maximum found, sum, if all positive, etc. l idiom n before loop declare variable to keep track, initialize it n inside loop, update tracking variable, use branching if necessary to examine n after loop, use the tracking variable that accumulated the result n example: cout << "Input number [0 to quit]: "; int max, n; cin >> n; max = n; while (n != 0 ) { cin >> n; if ( n > max) max = n; } cout << ”Maximum number: ” << max << endl; 7

Break and Continue with Iterative Constructs break - exits innermost loop int sum=0; while(sum < 100) { int i; cin >> i; if (i< 0) { cout << ”found negative number\n”; break; } sum +=i; } continue - skip the remaining statements and start a new iteration (evaluate expression) int sum=0; for (int i = 0; i > intVar; if(intVar < 0) continue; sum +=i; } avoid break/continue with loops as they make code less readable (avoid regular loop exit) n first try to code loop without them 8

Nesting of Iterative Constructs l iterative constructs can be nested: one iterative construct may be inside the body of another l example: for (int i = 0; i < 10; ++i) // outer loop for (int j = 0; j < 10; ++j) // inner loop cout << i << j << endl; what would this code output? note, there is no need for curly brackets l nesting may be more than two loops deep l for/while/do-while can be mixed in nesting l besides nested loops, loop body may contain other code n including branching constructs: a branching construct nested in the loop 9

Iteration key points l make sure there is a statement that will eventually nullify the iteration criterion (i.e., the loop must stop) l make sure that initialization of any loop counters or iterators is properly performed l have a clear purpose for the loop n document the purpose of the loop and how the body of the loop advances the purpose of the loop 10