While loop in Java

A while loop repeatedly executes the same set of instructions until the controlling condition is true.

1. Syntax

Its general form is:

  • condition is any boolean expression.
  • Loop body is executed until the condition is true. Once it becomes false, statements just after the loop body are executed.
  • If the loop body contains only one statement then curly brackets are not required. If it contains more than one statement then the curly braces are necessary.
  • A null statement(only semi-colon) is also a valid loop body.

2. Control Flow Diagram of While Loop

control flow in while loop
while loop

3. An example

The code below computes the sum of first 100 integers and then prints it: 

The output of the above code is:

Let’s see how this code works :

  • First the value of sum and i, both are assigned to 0.
  • After 1st iteration: sum = sum + i = 0 + 0 = 0, i = i+1 = 0+1 = 1
  • After 2nd iteration: sum = sum + i = 0 + 1 = 1, i = i+1 = 1+1 = 2
  • After 3rd iteration: sum = sum + i = 1 + 2 = 3, i = i+1 = 2+1 = 3
  • After 4th iteration: sum = sum + i = 3 + 3 = 6, i = i+1 = 3+1 = 4
  • After 5th iteration: sum = sum + i = 6 + 4 = 10, i = i+1 = 4+1 = 5
  • .
  • .
  • After 100th iteration: sum = sum + i = 4950 + 100 = 5050, i = i+1 = 100+1 = 101
  • Since i = 101, the condition i<=100 becomes false and loop breaks. The next line of the code is a print statement which prints the value of the computed sum.

4. Empty loop body

As stated earlier, a null statement is also a valid loop body. Sometimes the control expression can itself handle a lot of details, in those cases loop bodies are unnecessary. Like in the example below, a timer is set to 30; when it decreases down to 0, the print statements are triggered.

The output is :

5. Infinite while loop

There are situations where it is desirable to run a loop for as long as possible.

  • Like polling, when you continuously check for resources if they are available or not.
  • Or continuously taking input from the keyboard.

The code below, continuously takes input from the keyboard character by character.

The output is :

This example is a bit involved. Lets understand this, one at a time:

  • System.in.read() reads input from the keyboard, character by character and returns them as integers. We cast them again to characters. If the character inserted was not a newline(‘\n’) character, it is printed back.
  • throws java.io.IOException was needed  to handle input errors because it uses System.in.read().
  • Even if you feed a lot of characters at once, it will read them one by one(like IS Great was fed).
  • When you press ctrl + C  the code terminates.

6. Processing Arrays with while loop

Loops give you a lot of power when you have to do something repititive. In the example below we replace the value of an array element by its square.

The output is:

You can use nested while loops to process multi-dimensional arrays.

7. Nesting While loops

If one while loop contains another while loop inside its body then the while loops are said to be nested. This nesting can go on upto any level. The code below shows two-level nesting i.e two while loops are nested one-inside another:

The output is:

Note that you could have easily replaced any of the while loops with some other loop, say for-loop or do-while loop.

8. Use of break in a while loop

There are occasions when you no longer want to iterate over all the elements because some condition has already been met. In those cases, you have to terminate the loop prematurely. break is a keyword used specifically for this purpose.

The output is :

break, takes you out of the loop by one level. Had there been another loop surrounding the while loop in the previous example, internal loop would have been broken but the control would have still been surrounded by the external loop. Depending on the need, you might have used another break.

9. Use of continue in a while loop

Sometimes you want to skip rest of the instructions in a loop but you still want to continue running the loop, continue is a statement made specifically for this purpose.

The output is :

In case when i is divisible by 2, the continue statement sends the control to the start of the loop where the condition is checked. Then the statements in the while loop execute again from the start.

10. Labelled while loop

You can label a while loop using any valid Java token followed by a colon. Like in:

Labelling can be used for many uses. For example, break with label can be used to break out of a series of nested loops to the end of a labelled loop.

The output is :



Note for C/ C++ programmers

In Java(unlike C/C++), condition can only be a boolean expression i.e. it should evaluate to literal true or false and nothing else. (In C/C++ any non-zero or non-NULL value acts as true and NULL and zero act as false. )