while

Figure 184. A while loop Slide presentation Create comment in forum
int repetitions = 3; 
int loopCounter = 1; 

while (loopCounter <= repetitions ) {
   System.out.println("Do not copy!"); 
   loopCounter++; 
}
Do not copy!
Do not copy!
Do not copy!
A while loop

The code block will be repeated this number of times.

Helper variable keeping track of repetitions.

Condition to be checked prior each execution.

Code block of statement(s) to be repeated.

Helper variable being incremented during each iteration.


Figure 185. Combining increment and termination condition Slide presentation Create comment in forum
int repetitions = 3;
int loopCounter = 1;

while (loopCounter++ <= repetitions) {
   System.out.println("Do not copy!");
}
Do not copy!
Do not copy!
Do not copy!

Figure 186. while syntax Slide presentation Create comment in forum
while (booleanExpression)
   (block | statement)

Figure 187. Empty while body Slide presentation Create comment in forum
int threeSeries = 1;

while ((threeSeries *=3) < 100);

System.out.println(threeSeries);

Exercise: Guess resulting output.