With the while-loop in Java, you can execute state­ments until a ter­min­a­tion condition occurs. This loop form can also be combined and nested.

Web hosting
The hosting your website deserves at an un­beat­able price
  • Loading 3x faster for happier customers
  • Rock-solid 99.99% uptime and advanced pro­tec­tion
  • Only at IONOS: up to 500 GB included

What is the while-loop for Java?

The while-loop provides Java users with the ability to execute any number of state­ments as long as a pre­vi­ously defined condition is true. A count variable is not required, so the ter­min­a­tion condition can also be defined sep­ar­ately.

The ter­min­a­tion condition works after the Boolean. This means that the while-loop in Java executes the stored state­ments as long as the condition specified in the header is true. If the ter­min­a­tion condition changes to 'false' for the first time, the loop is ter­min­ated. Because the ter­min­a­tion condition is already set in the head of the while-loop in Java, it is called a head-con­trolled loop.

How is the while-loop con­struc­ted in Java?

The structure of a while-loop in Java doesn’t usually change too much. While the syntax remains the same, only the state­ments and the ter­min­a­tion condition change. First the term 'while' in­tro­duces the loop, then the ter­min­a­tion condition follows in brackets and finally one or more state­ments in curly brackets. This is what a while-loop looks like in Java source code:

while ( termination condition) {
Statement 
}

The flow always follows this order:

  1. The program checks the condition.
  2. If it is met, the Java ap­plic­a­tion executes the statement.
  3. Then the program jumps back to the beginning.
  4. Again, the condition is checked.
  5. If it is now 'false', the program exits the while-loop in Java and continues through the code.

Example of a simple loop

A simple example of a while-loop in Java is a counter that counts up to a certain value. It does this exactly until the specified value (the ter­min­a­tion condition) is reached. Written out, it looks like this:

int i = 0;
while ( i <= 5 ) {
System.out.println ( "The current value is at: " + i );
i++;
}

The variable 'i' starts at zero in this setup. It should now increase by 1 until it reaches the value '5'. This is done by the Java operator ++. In the next round, when the value would be '6', the program ter­min­ates and the while-loop in Java is ter­min­ated. This is indicated by the Java command System.out.println the number series 1, 2, 3, 4, 5.

Avoiding endless loops

Although the while-loop in Java is simple in structure, it does have a few potential error sources that can cause the loop fail. If an important piece of in­form­a­tion is missing or the ter­min­a­tion condition cannot be fulfilled, you’ll get an infinite loop. This either causes a crash or stops the program at some point. This happens, for example, if the check of the ter­min­a­tion condition always returns 'true' or the targeted value cannot be reached.

The dif­fer­ence between while and do-while in Java

In addition to the while-loop in Java, there is also the do-while-loop. This not only has a similar name, but also works almost exactly like the while-loop. The main dif­fer­ence is that the head-con­trolled while-loop checks the ter­min­a­tion condition in advance, and the foot-con­trolled do-while-loop waits until the state­ments have been executed at least once.

This can be seen in the following structure:

int i = 0;
do {
System.out.println ( "The current value is at: " + i );
i++;
}
while ( i <= 5 );

Here, the code is executed at least once and then the condition is checked af­ter­wards.

Nesting while-loops in Java

You also have the option of nesting several while-loops in Java. An example with two counters looks like this:

int i = 0;
while ( i 
System.out.println ( "The current value is: " + i );
i++;
int Intermediate step = 0;
while ( intermediate step <= 3 ) {
System.out.println ( "The current intermediate value is at: " + intermediate step );
IntermediateStep++;
}
}

So, first the outer while-loop is checked by the Java program. If it is 'true', the program jumps to the inner loop and checks it as well. Now the inner loop is checked until its ter­min­a­tion condition is 'false'. Then the program switches back to the outer loop.

The dif­fer­ence between for and while in Java

Besides the while-loop for Java presented here and the do-while-loop also taken up, there is also the for-loop. This resembles the Java while-loop at first sight, since it too repeats a code until the defined ter­min­a­tion condition becomes 'false'. Unlike the while-loop, however, the for loop still has a run variable that is ini­tial­ised before the ter­min­a­tion condition.

Go to Main Menu