For & do/while Loops.

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
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.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Objectives You should be able to describe:
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
© 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.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Examples from “c++ how to program” book. SELECTIONS WITH IF-ELSE Example: if ( grade >= 60) cout = 60) cout
Chapter 5: Control Structures II (Repetition)
Chapter 4 Program Control Statements
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
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.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
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.
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.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Control Structures RepetitionorIterationorLooping Part I.
Overview Go over parts of quiz? Another iteration structure for loop.
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
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.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
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.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
COMP Loop Statements Yi Hong May 21, 2015.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
01/05/100 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Branching Constructs Review
Control Structures II (Repetition)
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 2.2 Control Structures (Iteration)
Control Statements Kingdom of Saudi Arabia
Programming Fundamentals
While Loops.
Conditinoal Constructs Review
Arrays, For loop While loop Do while loop
Conditinoal Constructs Review
3 Control Statements:.
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Chapter 6: Repetition Statements
Control Structures Part 1
Alternate Version of STARTING OUT WITH C++ 4th Edition
2.6 The if/else Selection Structure
do/while Selection Structure
Repetition Statements (Loops) - 2
Control Statements Paritosh Srivastava.
Presentation transcript:

for & do/while Loops

Purpose: Improved syntax for Counter-Controlled Loops for loops Purpose: Improved syntax for Counter-Controlled Loops

for loops c = start; // could be many lines while (c <= stop) { c += increment; }

for loops Syntax: for (initStmt; condExpr; incStmt) body Where: - body is a single statement with a semicolon, or a Block { } .

for loops Semantics: for (initStmt; condExpr; incStmt) body 1. Execute initStmt; 2. When condExpr is FALSE, skip the body. When condExpr is TRUE: Execute the body Execute the incStmt Repeat step 2.

for loops Style: for (initStmt; condExpr; incStmt) oneStatement ; { }

for loops Example 1: (warning: poor style!) int i; for (i=2; i<=12; i+=2) cout << i << ” ”; cout << ”\ni =” << i; Prints: 2 4 6 8 10 12 i = 14

for loops Example 1: (style corrected) int i; for (i=2; i<=12; i+=2) cout << i << ” ”; cout << ”\ni =” << i; Prints: 2 4 6 8 10 12 i = 14

for loops Example 2: counting backwards for (i=10; i>=1; i--) { cout << i << ” ”; } cout << ”\ni =” << i; Prints: 10 9 8 7 6 5 4 3 2 1 i = 0

for loops Example 3: zero iterations for (i=10; i<=5; i++) { cout << i << ” ”; } cout << ”\ni =” << i; Prints: i = 10

for loops Example 4: sum all numbers 2 to 10 int i, sum = 0; for (i=2; i<=10; i++) { sum = sum + i; } cout << sum; Prints: 54

for loops Alternate Control Variable Declaration: int i; for (i=1; i<=10; i++) { } Can be written as: for (int i=1; i<=10; i++) { // but i is undefined after the loop

for loops Nested for loops: Prints: for (int i=1; i<=3; i++) 1 5 for (int j=5; j<=7; j++) 1 6 cout << i << ” ” 1 7 << j << endl; 2 5 2 6 2 7 3 5 3 6 3 7

do/while loops Purpose: Same as a while loop, except the condition is checked at the bottom instead of the top. Use when: there should be at lease one iteration.

do/while loops Syntax: do body while (condExpr); Where: - body is a single statement with a semicolon, or a Block { } .

do/while loops Semantics: do body while (condExpr); 1. Execute the body 2. When the condExpr is TRUE, repeat step 1 When the condExpr is FALSE, skip body

do/while loops Style 1: do oneStatement; while (condExpr);

do/while loops Style 2: common do { statements } while (condExpr);

do/while loops Style 3: less common do { oneStatement; } while (condExpr);

do/while loops Example 1: validation loop int n; do { cout << ”Enter negative number: ”; cin >> n; if (n >= 0) cout << ”Try again!\n”; } while (n >= 0);

do/while loops Example 2: menu loop string option; do { // print menu, with X=exit cout << ”Enter menu option: ”; cin >> option; // switch or nested if/else’s } while (option != ”X”);