1 do-while statement 2 do-while statement is a looping control structure in which the loop condition...
TRANSCRIPT
![Page 1: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/1.jpg)
1
do-while Statement
![Page 2: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/2.jpg)
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.
![Page 3: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/3.jpg)
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.
![Page 4: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/4.jpg)
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
![Page 5: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/5.jpg)
while v. do-while CCL while v. do-while CCL
sum = 0;
counter = 1;
while (counter <= n)
{
sum = sum + counter;
counter++;
}
Pretest Loop
sum = 0;
counter = 1;
do
{
sum = sum + counter;
counter++;
} while (counter <= n)
// Note Sum=1 if n=0
Posttest Loop Loop always executes at
least once
![Page 6: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/6.jpg)
6
for Statement
![Page 7: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/7.jpg)
7
A Count-Controlled Loop
SYNTAX
for ( initialization ; test expression ; update )
{
0 or more statements to repeat
}
![Page 8: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/8.jpg)
8
The for loop contains
an initialization
an expression to test for continuing
an update to execute after each iteration of the body
![Page 9: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/9.jpg)
9
Example of Repetition
for ( int num = 1 ; num <= 3 ; num++ )
{
println(num + " Potato");
}
![Page 10: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/10.jpg)
10
Example of Repetition num
var num;
for ( num = 1 ; num <= 3 ; num++ )
println(num + " Potato");
OUTPUT
?
![Page 11: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/11.jpg)
11
Example of Repetition num
OUTPUT
1
var num;
for ( num = 1 ; num <= 3 ; num++ )
println(num + " Potato");
![Page 12: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/12.jpg)
12
Example of Repetition num
OUTPUT
1
var num;
for ( num = 1 ; num <= 3 ; num++ )
println(num + " Potato");
true
![Page 13: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/13.jpg)
13
Example of Repetition num
var num;
for ( num = 1 ; num <= 3 ; num++ )
println(num + " Potato");
OUTPUT
1
1Potato
![Page 14: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/14.jpg)
14
Example of Repetition num
OUTPUT
2
var num;
for ( num = 1 ; num <= 3 ; num++ )
println(num + " Potato");
1Potato
![Page 15: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/15.jpg)
15
Example of Repetition num
OUTPUT
2
true
1Potato
var num;
for ( num = 1 ; num <= 3 ; num++ )
println(num + " Potato");
![Page 16: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/16.jpg)
16
Example of Repetition num
var num;
for ( num = 1 ; num <= 3 ; num++ )
println(num + " Potato");
OUTPUT
2
1Potato
2Potato
![Page 17: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/17.jpg)
17
Example of Repetition num
OUTPUT
3
var num;
for ( num = 1 ; num <= 3 ; num++ )
println(num + " Potato");
1Potato
2Potato
![Page 18: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/18.jpg)
18
Example of Repetition num
OUTPUT
3
true
1Potato
2Potato
var num;
for ( num = 1 ; num <= 3 ; num++ )
println(num + " Potato");
![Page 19: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/19.jpg)
19
Example of Repetition num
var num;
for ( num = 1 ; num <= 3 ; num++ )
println(num + " Potato");
OUTPUT
3
1Potato
2Potato
3Potato
![Page 20: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/20.jpg)
20
Example of Repetition num
OUTPUT
4
var num;
for ( num = 1 ; num <= 3 ; num++ )
println(num + " Potato");
1Potato
2Potato
3Potato
![Page 21: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/21.jpg)
21
Example of Repetition num
OUTPUT
4
false
1Potato
2Potato
3Potato
var num;
for ( num = 1 ; num <= 3 ; num++ )
println(num + " Potato");
![Page 22: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/22.jpg)
22
Example of Repetition num
When the loop control condition is evaluated and has value false, theloop is said to be “satisfied” and control passes to the statementfollowing the For statement.
4
falsevar num;
for ( num = 1 ; num <= 3 ; num++ )
println(num + " Potato");
![Page 23: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/23.jpg)
23
The output was:
1Potato2Potato3Potato
![Page 24: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/24.jpg)
24
for (var count = 4 ; count > 0 ; count-- )
{
println(count);
}
println(“Done”);
Count-controlled Loop
OUTPUT: 4321Done
![Page 25: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/25.jpg)
25
What is output?
for ( var count = 0 ; count < 10 ; count++ )
{
println('*');
}
![Page 26: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/26.jpg)
26
OUTPUT
**********
NOTE: the 10 asterisks are all on one line. Why?
![Page 27: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/27.jpg)
Count Control Loop Example
Display integers and their squares from 1 through 10.
for (var i = 1; i <= 10; i++) println(i + " " + i*i);
![Page 28: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/28.jpg)
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);
![Page 29: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/29.jpg)
For example
Display integers and their squares from 10 down to 1.
for (var i = 10; i >= 1; i--) println(i + " " + i*i);
![Page 30: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/30.jpg)
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));
![Page 31: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/31.jpg)
Compute and return n! = 1 2 3 ... n.
var product = 1;
for (var i = 2; i <= n; i++)
product = product * i;
For example
![Page 32: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/32.jpg)
32
What output from this loop?
for (var count = 0; count < 10; count++) ;
{
println(“”);
}
![Page 33: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/33.jpg)
33
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
![Page 34: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/34.jpg)
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
![Page 35: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/35.jpg)
Table of 2nTable of 2n
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;
}
![Page 36: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/36.jpg)
Eliminating WhileExpressionEliminating WhileExpression
The while condition is also optional If omitted the value defaults to true
for ( ; ; )
println("Hi");
while (1)
println("Hi");
![Page 37: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/37.jpg)
Changing the values of any variables involved in the loop condition inside the body of the loop may change the number of repetitions & may result in an infinite loop
for (i = 1; i <= 10; i++)
{
println(i);
i++;
}
Monkeying with LCVs: PPPMonkeying with LCVs: PPP
![Page 38: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/38.jpg)
38
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
![Page 39: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/39.jpg)
Use break As a Last ResortUse break As a Last Resort
It can become a crutch 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++;
}
![Page 40: 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](https://reader033.vdocument.in/reader033/viewer/2022042522/5697c0081a28abf838cc6edf/html5/thumbnails/40.jpg)
40
Continue Statement
continue is valid only within loops
terminates the current loop iteration, but not the entire loop
in a For or While, continue causes the rest of the body statement to be skipped--in a For statement, the update is done
in a Do-While, the exit condition is tested, and if true, the next loop iteration is begun