... while
to output the numbers 1–10.
Fig. 5.7
1 // Fig. 5.7: DoWhileTest.java
2 // do…while iteration statement.
3
4 public class DoWhileTest {
5 public static void main(String[] args) {
6 int counter = 1;
7
8 do {
9 System.out.printf("%d ", counter);
10 ++counter;
11 } while (counter <= 10);
12
13 System.out.println();
14 }
15 }
1 2 3 4 5 6 7 8 9 10
do…while iteration statement.
Line 6 declares and initializes control variable counter
. Upon entering the do…while
statement, line 9 outputs counter
’s value and line 10 increments counter
. Then the program evaluates the loop-continuation test at the bottom of the loop (line 11). If the condition is true
, the loop continues at the first body statement (line 9). If the condition is false
, the loop terminates ...
Get Java How to Program, Early Objects, 11th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.