Java Reference
In-Depth Information
1 2 3 4 5 6 7 8 9 10
Fig. 5.7 | do while repetition statement. (Part 2 of 2.)
Line 8 declares and initializes control variable counter . Upon entering the do while
statement, line 12 outputs counter 's value and line 13 increments counter . Then the pro-
gram evaluates the loop-continuation test at the bottom of the loop (line 14). If the condi-
tion is true , the loop continues at the first body statement (line 12). If the condition is false ,
the loop terminates and the program continues at the next statement after the loop.
UML Activity Diagram for the do while Repetition Statement
Figure 5.8 contains the UML activity diagram for the do while statement. This diagram
makes it clear that the loop-continuation condition is not evaluated until after the loop
performs the action state at least once . Compare this activity diagram with that of the while
statement (Fig. 4.6).
Display the
counter value
System.out.printf( “%d ” , counter );
Increment the
control variable
++counter
[counter <= 10]
Determine whether
looping should
continue
[counter > 10]
Fig. 5.8 | do while repetition statement UML activity diagram.
Braces in a do while Repetition Statement
It isn't necessary to use braces in the do while repetition statement if there's only one
statement in the body. However, many programmers include the braces, to avoid confu-
sion between the while and do while statements. For example,
while ( condition )
is normally the first line of a while statement. A do while statement with no braces
around a single-statement body appears as :
 
 
Search WWH ::




Custom Search