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.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
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.
Computer Science 1620 Loops.
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.
1 CIS Jan Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.
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.
Chapter 5: Control Structures II (Repetition)
Chapter 5: Loops and Files.
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 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.
Chapter 5: Control Structures II (Repetition)
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
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.
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 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.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
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 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.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Overview Go over parts of quiz? Another iteration structure for loop.
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.
Loops and Files. 5.1 The Increment and Decrement Operators.
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.
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)
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.
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.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
While ( number
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Computer Programming -1-
REPETITION CONTROL STRUCTURE
Chapter 2.2 Control Structures (Iteration)
Additional Control Structures
Chapter 7 Additional Control Structures
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Alternate Version of STARTING OUT WITH C++ 4th Edition
PROGRAM FLOWCHART Iteration Statements.
Presentation transcript:

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

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

4 Do-While Loop When the expression is tested and found to be false, the loop is exited and control passes to the statement that follows the do-while statement. Statement Expression DO WHILE FALSE TRUE

while v. do-while CCL sum = 0; counter = 1; while (counter <= n) { sum = sum + counter; counter++; } l Pretest Loop sum = 0; counter = 1; do { sum = sum + counter; counter++; } while (counter <= n) // Note Sum=1 if n=0 l Posttest Loop l Loop always executes at least once

6 for Statement

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

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

9 Example of Repetition for ( int num = 1 ; num <= 3 ; num++ ) { println(num + " Potato"); }

10 Example of Repetition num var num; for ( num = 1 ; num <= 3 ; num++ ) println(num + " Potato"); OUTPUT ?

11 Example of Repetition num OUTPUT 1 var num; for ( num = 1 ; num <= 3 ; num++ ) println(num + " Potato");

12 Example of Repetition num OUTPUT 1 var num; for ( num = 1 ; num <= 3 ; num++ ) println(num + " Potato"); true

13 Example of Repetition num var num; for ( num = 1 ; num <= 3 ; num++ ) println(num + " Potato"); OUTPUT 1 1Potato

14 Example of Repetition num OUTPUT 2 var num; for ( num = 1 ; num <= 3 ; num++ ) println(num + " Potato"); 1Potato

15 Example of Repetition num OUTPUT 2 true 1Potato var num; for ( num = 1 ; num <= 3 ; num++ ) println(num + " Potato");

16 Example of Repetition num var num; for ( num = 1 ; num <= 3 ; num++ ) println(num + " Potato"); OUTPUT 2 1Potato 2Potato

17 Example of Repetition num OUTPUT 3 var num; for ( num = 1 ; num <= 3 ; num++ ) println(num + " Potato"); 1Potato 2Potato

18 Example of Repetition num OUTPUT 3 true 1Potato 2Potato var num; for ( num = 1 ; num <= 3 ; num++ ) println(num + " Potato");

19 Example of Repetition num var num; for ( num = 1 ; num <= 3 ; num++ ) println(num + " Potato"); OUTPUT 3 1Potato 2Potato 3Potato

20 Example of Repetition num OUTPUT 4 var num; for ( num = 1 ; num <= 3 ; num++ ) println(num + " Potato"); 1Potato 2Potato 3Potato

21 Example of Repetition num OUTPUT 4 false 1Potato 2Potato 3Potato var num; for ( num = 1 ; num <= 3 ; num++ ) println(num + " Potato");

22 Example of Repetition num When the loop control condition is evaluated and has value false, the loop is said to be “satisfied” and control passes to the statement following the For statement. 4 false var num; for ( num = 1 ; num <= 3 ; num++ ) println(num + " Potato");

23 The output was: 1Potato 2Potato 3Potato

24 for (var count = 4 ; count > 0 ; count -- ) { println(count); } println(“Done”); Count-controlled Loop OUTPUT: Done

25 What is output? for ( var count = 0 ; count < 10 ; count++ ) { println('*'); }

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

Count Control Loop Example Display integers and their squares from 1 through 10. for (var i = 1; i <= 10; i++) println(i + " " + i*i);

For example Display even integers and their squares from 1 through 10. for (var i = 2; i <= 10; i = i+2) println(i + " " + i*i);

For example Display integers and their squares from 10 down to 1. for (var i = 10; i >= 1; i--) println(i + " " + i*i);

For example Find square roots of 1.1, 1.2, 1.3,..., 2.0 for (var x = 1.1; x <= 2.0; x =x+0.1) println(x + " " + sqrt(x));

Compute and return n! = 1  2  3 ...  n. var product = 1; for (var i = 2; i <= n; i++) product = product * i; For example

32 What output from this loop? for (var count = 0; count < 10; count++) ; { println(“  ”)  ; }

33 l no output from the for loop! Why? l the ; right after the ( ) means that the body statement is a null statement l in general, the Body of the for loop is whatever statement immediately follows the ( ) l 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

Display all divisors of each integer from 1 through 50 for (int num = 1; num <= 50; num++) { cout << num << " has divisors:\n\t''; for (int div = 1; div <= num/2; div++) if (num % div == 0) cout << div << ", ''; cout << num << endl; }// See divisors.cpp

Table of 2 n const int tableSize = 20; long valueSquared = 1; cout << "n" << " " << "2**n" << endl; for (int n = 0; n <= tableSize; ++n) { cout << n << " " << valueSquared << endl; valueSquared = valueSquared * 2; }

Eliminating WhileExpression l The while condition is also optional l If omitted the value defaults to true for ( ; ; ) println("Hi"); while (1) println("Hi");

n Changing the values of any variables n involved in the loop condition n inside the body of the loop n may change the number of repetitions n & may result in an infinite loop for (i = 1; i <= 10; i++) { println(i); i++; } Monkeying with LCVs: PPP

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

Use break As a Last Resort l It can become a crutch l Think carefully about loop design for loop on right is better i = 1; while (1)for (i = 1; i <= 5; i++) { println(i); println(i); if (i == 5) break; i++; }

40 Continue Statement continue is valid only within loops l terminates the current loop iteration, but not the entire loop l in a For or While, continue causes the rest of the body statement to be skipped--in a For statement, the update is done l in a Do-While, the exit condition is tested, and if true, the next loop iteration is begun