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.

Slides:



Advertisements
Similar presentations
CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Advertisements

LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
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.
Computer Science 1620 Loops.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
1 CIS Jan Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
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.
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
1 Chapter 7 Additional Control Structures. 2 Knowledge Goals Understand the role of the switch statement Understand the purpose of the break statement.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Multi-alternative Selection Both the if clause and the else clause of an if...else statement can contain any kind of statement, including another selection.
Chapter 5: Control Structures II (Repetition)
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
Introduction to Java and Software Design Dale Weems Headington Chapter 10 Additional Control Structures and Exceptions Slides by Sylvia Sorkin, The Community.
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.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
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.
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 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
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.
Chapter 7 Additional Control Structures. Chapter 7 Topics l Switch Statement for Multi-Way Branching l Do-While Statement for Looping l For Statement.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
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 05 (Part III) Control Statements: Part II.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
Control Structures RepetitionorIterationorLooping Part I.
1 CS161 Introduction to Computer Science Topic #8.
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 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
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.
1 Looping Dale/Weems/Headington. 2 KA/JS/P Warning l Save your work often! l In the Khan Academy, JavaScript environment, infinite loops will lock up.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
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)
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
CMSC 1041 More Loops ‘for’ loops and ‘do-while’ loops.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Computer Programming -1-
Review 1.
REPETITION CONTROL STRUCTURE
Chapter 2.2 Control Structures (Iteration)
Control Structures - Repetition
Additional Control Structures
Chapter 7 Additional Control Structures
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Looping III (do … while statement)
Control Statements Paritosh Srivastava.
Presentation transcript:

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 ( Expression ) ; Loop body statement can be a single statement or a block.

2 do { cin >> response ; // skips leading whitespace if ( ( response != ‘y’ ) && ( response != ‘n’ ) ) cout << “Please type y or n : “ ; } while ( ( response != ‘y’ ) && ( response != ‘n’ ) ) ; Condition is tested first Statement will be executed at least once { } cannot be omitted even with one statement Semicolon at end of the condition expression Function Using Do-While 2

3 Do-While Loop vs. While Loop POST-TEST loop (exit-condition) The looping condition is tested after executing the loop body. Loop body is always executed at least once. PRE-TEST loop (entry-condition) The looping condition is tested before executing the loop body. Loop body may not be executed at all.

4 A Count-Controlled Loop SYNTAX for ( initialization ; test expression ; update ) { 0 or more statements to repeat }

5 The for loop contains an initialization an expression to test for continuing an update to execute after each iteration of the body

6 Example of Repetition int num; for ( num = 1 ; num <= 3 ; num++ ) { cout << num << “Potato” << endl; }

7 Example of Repetition num int num; for ( num = 1 ; num <= 3 ; num++ ) cout << num << “Potato” << endl; OUTPUT ?

8 The output was: 1Potato 2Potato 3Potato

9 int count ; for ( count = 4 ; count > 0 ; count -- ) cout << count << endl; cout << “Done” << endl; Count-controlled Loop OUTPUT: Done

10 What is output? int count; for ( count = 0 ; count < 10 ; count++ ) { cout << “  ”  ; }

11 OUTPUT ********** NOTE: the 10 asterisks are all on one line. Why?

12 What output from this loop? int count; for (count = 0; count < 10; count++) ; { cout << “  ”  ; }

13 no output from the for loop! Why? the ; right after the ( ) means that the body statement is a null statement in general, the Body of the for loop is whatever statement immediately follows the ( ) that statement can be a single statement, a block, or a null statement actually, the code outputs one * after the loop completes its counting to 10 OUTPUT

14 Switch Statement Is a selection control structure for multi-way branching. SYNTAX switch ( IntegralExpression ) { case Constant1 : Statement(s); // optional case Constant2 : Statement(s); // optional. default :// optional Statement(s); // optional }

15 float weightInPounds = ; char weightUnit ;... // user enters letter for desired weightUnit switch ( weightUnit ) { case ‘P’ : case ‘p’ : cout << weightInPounds << “ pounds “ << endl ; break ; case ‘O’ : case ‘o’ : cout << 16.0 * weightInPounds << “ ounces “ << endl ; break ; case ‘K’ : case ‘k’ : cout << weightInPounds / 2.2 << “ kilos “ << endl ; break ; case ‘G’ : case ‘g’ : cout << * weightInPounds << “ grams “ << endl ; break ; default : cout << “That unit is not handled! “ << endl ; break ; } 15

16 Switch Statement the value of IntegralExpression (of char, short, int, long or enum type ) determines which branch is executed case labels are constant ( possibly named ) integral expressions. Several case labels can precede a statement

17 Break Statement break statement can be used with Switch or any of the 3 looping structures it causes an immediate exit from the Switch, While, Do-While, or For statement in which it appears if the break is inside nested structures, control exits only the innermost structure containing it