while

Figure 189. A while loop Slide presentation Create comment in forum
final int repetitions = scan.nextInt(); 
int loopCount = 1; 

while (loopCount <= repetitions ) {
   System.out.println("Do not copy!"); 
   loopCount++; 
}
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 190. Combining increment and termination condition Slide presentation Create comment in forum
System.out.print("Enter repetitions: ");
final int repetitions = scan.nextInt();
int loopCounter = 1;

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

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

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

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

System.out.println(threeSeries);

Exercise: Guess resulting output.