Objectives
while

(A)
while (loop-continuation-condition) {
// loop-body;
Statement(s);
}
(B)
int
count = 0;
while
(count < 100) {
System.out.println("Welcome to
Java!");
count++;
}
Caution
Don’t use floating-point values for equality checking in a loop control. Since floating-point values are approximations, using them could result in imprecise counter values and inaccurate results. This example uses int value for data. If a floating-point type value is used for data, (data != 0) may be true even though data is 0.
// data should be zero
double data = Math.pow(Math.sqrt(2), 2) - 2;
if (data == 0)
System.out.println("data is zero");
else
System.out.println("data is not zero");
do-while

do {
// Loop body;
Statement(s);
} while (loop-continuation-condition);
for Loops

(A)
for (initial-action; loop-continuation-condition; action-after-each-iteration) {
// loop body;
Statement(s);
}
(B)
int i;
for (i = 0; i < 100; i++) {
System.out.println(
"Welcome to Java!");
}
Note
The initial-action in a for loop can be a list of zero or more comma-separated expressions. The action-after-each-iteration in a for loop can be a list of zero or more comma-separated statements. Therefore, the following two for loops are correct. They are rarely used in practice, however.
for (int i = 1; i < 100; System.out.println(i++));
for (int i = 0, j = 0; (i + j < 10); i++, j++) {
// Do something
}
Note
If the loop-continuation-condition in a for loop is omitted, it is implicitly true. Thus the statement given below in (a), which is an infinite loop, is correct. Nevertheless, it is better to use the equivalent loop in (b) to avoid confusion:

Which Loop to Use?
The three forms of loop statements, while, do-while, and for, are expressively equivalent; that is, you can write a loop in any of these three forms. For example, a while loop in (a) in the following figure can always be converted into the following for loop in (b):

A for loop in (a) in the following figure can generally be converted into the following while loop in (b) except in certain special cases (see Review Question 3.19 for one of them):

Recommendations
Use the one that is most intuitive and comfortable for you. In general, a for loop may be used if the number of repetitions is known, as, for example, when you need to print a message 100 times. A while loop may be used if the number of repetitions is not known, as in the case of reading the numbers until the input is 0. A do-while loop can be used to replace a while loop if the loop body has to be executed before testing the continuation condition.
Caution
Adding a semicolon at the end of the for clause before the loop body is a common mistake, as shown below:
for (int i=0; i<10; i++); ß Logic error
{
System.out.println("i is " + i)
}
Similarly, the following loop
is also wrong:
int i=0;
while (i < 10); ß Logic error
{
System.out.println("i is " + i);
i++;}
In the case of the do loop, the following semicolon is needed to end the loop.
int i=0;
do {
System.out.println("i is " + i);
i++;
} while (i<10);