Professional Documents
Culture Documents
04 Control Structures II
04 Control Structures II
Chapter Objectives
2
Why Is Repetition Needed?
• There are many situations in which the same statements need to be executed
several times
• Example
– Formulas used to find average grades for students in a class
3
The while Looping (Repetition) Structure
• Syntax
while (expression)
statement
• Expression is always true in an infinite loop
• Statements must change value of expression to false
4
The while Looping (Repetition) Structure …
5
The while Looping (Repetition) Structure …
Example 5-1
i = 0; //Line 1
while (i <= 20) //Line 2
{
System.out.print(i + " "); //Line 3
i = i + 5; //Line 4
}
System.out.println(); //Line 5
Output:
0 5 10 15 20
6
The while Looping (Repetition) Structure …
7
Counter-Controlled while Loop
• Used when exact number of data or entry pieces is
known
• General form:
8
Sentinel-Controlled while Loop
• Used when exact number of entry pieces is unknown but
last entry (special/sentinel value) is known
• General form:
9
Flag-Controlled while Loop
• Boolean value used to control loop
• General form:
10
EOF(End of File)-Controlled while Loop
11
EOF(End of File)-Controlled while Loop …
• The expression console.hasNext() evaluates to
true if there is an input in the input stream; otherwise it
returns false
• Expressions such as console.nextInt() act as the
loop condition
• A general form of the EOF-controlled while loop that
uses the Scanner object console to input data is of the
form:
12
EOF(End of File)-Controlled while Loop …
13
The for Looping (Repetition) Structure
14
The for Looping (Repetition) Structure
(continued)
• Execution
– Initial statement executes
– The loop condition evaluated
– If loop condition evaluates to true, execute for
loop statement and execute update statement
– Repeat until loop condition is false
15
The for Looping (Repetition) Structure …
16
The for Looping (Repetition) Structure …
System.out.println();
17
The for Looping (Repetition) Structure …
Example
1. The following for loop outputs the word Hello and a star (on separate
lines) five times:
for (i = 1; i <= 5; i++)
{
System.out.println("Hello");
System.out.println("*");
}
2.
for (i = 1; i <= 5; i++)
System.out.println("Hello");
System.out.println("*");
This loop outputs the word Hello five times and the star only once
18
The for Looping (Repetition) Structure …
19
The for Looping (Repetition) Structure …
20
The do…while Loop (Repetition) Structure
• Syntax:
21
do…while Loop (Post-test Loop)
22
break Statements
23
continue Statements
• Used in while, for, and do...while structures
• When executed in a loop, the remaining
statements in the loop are skipped; proceeds with
the next iteration of the loop
• When executed in a while/do…while
structure, expression evaluated immediately after
continue statement
• In a for structure, the update expression is
executed after the continue statement; then the
loop condition executes
24
Nested Control Structures
25
Nested Control Structures (Example)
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
System.out.print(" *");
System.out.println();
}
• Output:
*
**
***
****
*****
26
Chapter Summary
• Looping Mechanisms
– Counter-controlled while loop
– Sentinel-controlled while loop
– Flag-controlled while loop
– EOF-controlled while loop
– for loop
– do…while loop
• break statements
• continue statements
• Nested control structures
27