Java Reference
In-Depth Information
The important difference between the while and do-while loops involves when the
controlling Boolean expression is checked. With a while statement, the Boolean expres-
sion is checked before the loop body is executed. If the Boolean expression evaluates to
false , then the body is not executed at all. With a do-while statement, the body of the
Display 3.7 Demonstration of while Loops and do-while Loops (part 1 of 2)
1 public class WhileDemo
2{
3
public static void main(String[] args)
4
{
5
int countDown;
6
System.out.println("First while loop:");
7
countDown = 3;
8
while (countDown > 0)
9
{
10
System.out.println("Hello");
11
countDown = countDown - 1;
12
}
13
System.out.println("Second while loop:");
14
countDown = 0;
15
while (countDown > 0)
16
{
17
System.out.println("Hello");
18
countDown = countDown - 1;
19
}
20
System.out.println("First do-while loop:");
21
countDown = 3;
22
do
23
{
24
System.out.println("Hello");
25
countDown = countDown - 1;
26
} while (countDown > 0);
27
System.out.println("Second do-while loop:");
28
countDown = 0;
29
do
30
{
31
System.out.println("Hello");
32
countDown = countDown - 1;
33
} while (countDown > 0);
34
}
35
}
(continued)
Search WWH ::




Custom Search