Introduction
Repetition statements are also called iteration statements or loop statements . Repetition statements will repeat the same code multiple times in succession. The number of times is controlled by a loop expression. The set of instructions will be repeated as long as the evaluation of the loop expression is true.
Java provides three repetition statements: while, do…while and for.
While Statement
The while statement has the following form:
while (loop expression) { code to be executed; }
The instructions are executed as long as the loop expression evaluates as true. When the logical expression becomes false then the while loop ends.
The loop expression is evaluated when entering the while structure. To make this possible, all variables used in the logical expression, must be initialized before the start of the while loop. If the loop expression evaluates to false at the start of the loop, no instructions will be executed. After each iteration, the logical expression will be reevaluated. When the evaluation is still true, the loop instructions are executed again. When it becomes false, the loop ends. To ensure that the iteration ends, one or more control variables used in the loop expression, must get a new value inside the loop structure.
In the next program we calculate the sum (variable total) of the numbers 1 to 5 (variable number).
//WhileTest1 //Carlos De Backer public class WhileTest1{ public static void main (String[] args){ int number, total; number = 1; total = 0; while (number <= 5){ System.out.println("Iteration number : " + number); total = total + number; number++; } System.out.println("Total : "+ total); } }
Iteration number : 1 Iteration number : 2 Iteration number : 3 Iteration number : 4 Iteration number : 5 Total : 15
In the following animation we show how to print the numbers from 1 to 5 using the while statement:

The while loop can be used when the number of repetitions is unknown in advance and when the number of repetitions can be zero . The do .. while structure is very similar to the while structure and can be used when the number of repetitions is at least one.
Do…While Statement
The do … while structure has the following format:
do { code to be executed; } while (loop expression);
Since the loop expression is at the end of the structure, the statements inside the loop will be executed at least once. The next source code is an illustration of the use of the do .. while structure:
//DoWhileTest1 //Carlos De Backer public class DoWhileTest1{ public static void main (String[] args){ int number, total; number = 1; total=0; do{ total = total + number; System.out.println("Iteration number : "+ number); number++; } while (number <= 5); System.out.println("Total : " + total); } }
Iteration number : 1 Iteration number : 2 Iteration number : 3 Iteration number : 4 Iteration number : 5 Total : 15
For Statement
The while structure can be used in all situations. The for and do .. while statements are only made available to simplify some specific programming problems.
The for statement has the following format:
for (initialisation expression; termination expression; increment expression){ code to be executed; }
The initialisation expression is executed as the loop starts. The termination expression determines under which condition the loop should continue (expression evaluates true). When the termination expression evaluates to false, the loop ends. The increment expression is called after each iteration.
The following source code illustrates the use of the for statement:
//ForTest1 //Carlos De Backer public class ForTest1{ public static void main(String[] args){ int number, total; total = 0; for (number = 1; number <= 5; number++){ System.out.println("Iteration number : "+ number); total = total + number; } System.out.println("Total : "+ total); } }
Iteration number : 1 Iteration number : 2 Iteration number : 3 Iteration number : 4 Iteration number : 5 Total : 15
The for statement can best be used when the number of loops is fixed and known in advance.
The while loop can be used when the number of repetitions is unknown and can be zero. The do...while loop can be used when the number of repetitions is unknown and is at least one. The for loop can be used when the number of repetitions is fixed and known in advance.