Chapter 2
Chapter 2
Repetition
Objectives
Output is:
1 2 3 4 5 6 7 8 9 10
The while Statement (continued)
Output is:
10 9 8 7 6 5 4 3 2 1
The while Statement (continued)
Output is:
NUMBER SQUARE CUBE
------ ------ ----
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
The while Statement (continued)
Condition-controlled loop
Output is:
DEGREES DEGREES
CELSIUS FAHRENHEIT
------- ----------
5 41.00
10 50.00
15 59.00
20 68.00
25 77.00
30 86.00
35 95.00
40 104.00
45 113.00
50 122.00
Computing Sums and Averages Using
a while Loop
Computing Sums and Averages Using
a while Loop (continued)
Computing Sums and Averages Using
a while Loop (continued)
Computing Sums and Averages Using
a while Loop (continued)
Computing Sums and Averages Using
a while Loop (continued)
Accumulating statement
Computing Sums and Averages Using
a while Loop (continued)
Calculating an average
Sentinels
• Output is:
2 4 6 8 10 12 14 16 18 20
The for Statement (continued)
The for Statement (continued)
The for Statement (continued)
Comma-separated list
The for Statement (continued)
• Sample run:
i is now 1
j = 1 j = 2 j = 3 j = 4
i is now 2
j = 1 j = 2 j = 3 j = 4
i is now 3
j = 1 j = 2 j = 3 j = 4
i is now 4
j = 1 j = 2 j = 3 j = 4
i is now 5
j = 1 j = 2 j = 3 j = 4
Nested Loops (continued)
Nested Loops (continued)
The do-while Statement
The do-while Statement (continued)