Java Repetition Statements: while Loops
while Loops
A while loop is another kind of repetition statement where the code block will continually execute as long as an expression remains true. The general syntax is:
while (expression) {
statements;
}
A while loop must start with the while keyword which is followed by and expression to be evaluated. The expression can be simple or complex, but as long as it resolves to a boolean true the code block will be executed. Unlike the for loop, there is no variable increment involved so there must be another event within the code that will change the condition from true to false when at the appropriate time. This increment is typically placed within the code block. Let’s take a look at a while loop in practice:
int counter = 0; while (counter < 3) { counter++; System.out.println(counter); }
Here we have a variable called counter the has been defined and initialised with a value of 0, and we have used that variable within the expression that determines if the while loop with execute. The expression requires that the value of counter is less than 3 – if it is then the code block will be executed, but if it is not then the code block will be bypassed and the next statement after the while loop will be executed.
The very first time that the while loop runs, the variable counter will have a value of 0 which means the expression (counter < 3) will be true and therefore the code block will execute. Within the code block there is a statement that increments the value of counter followed by a statement that will output the current value of counter.
So, the next time the while loop runs, counter will have a value of 1 – can you see that we are getting closer to the expression (counter < 3) resolving to false? Here is the codes output:
run: 1 2 3 BUILD SUCCESSFUL (total time: 0 seconds)
The code block is executed three times, which makes sense since the expression (counter < 3) would be true for when counter has a value of 0, 1 and 2 – but when it is incremented from 2 to 3 the expression is no longer true and the code block is not executed.
When coding with while loops, like other loops, you need to be careful to avoid perpetual loops (loops that never end). For example:
int counter = 5; while (counter > 3) { counter++; System.out.println(counter); }
Here, the variable counter is initialised with a value of 5 and the expression will allow the code block to execute as long as counter is greater than 3. A problem emerges when we look into the code block and see that counter is incremented, this means that the expression (counter > 3) will ALWAYS be true resulting in a perpetual loop:
run: 6 7 8 9 10 11 12 ...
The actual output would be thousands of lines. Something to look out for and avoid!
Nested while loops
Like we saw with for loops earlier, you can nest while loops too:
int counter = 0; int internalCounter = 0; while (counter < 3) { counter++; System.out.println(counter); while (internalCounter < 2) { internalCounter++; System.out.println("I'm a nested while loop"); } internalCounter = 0; }
Here we have two variables, counter and internalCounter, both initialised to 0. We also have two while loops, one nested within the other. The outer loop will execute its code block provided the expression (counter < 3) remains true. That code block contains a statement the will increment counter (counter++;) as well as the nested or inner while loop whose condition is (internalCounter < 2).
run: 1 I'm a nested while loop I'm a nested while loop 2 I'm a nested while loop I'm a nested while loop 3 I'm a nested while loop I'm a nested while loop BUILD SUCCESSFUL (total time: 0 seconds)
In much the same as the the nested for loop example we saw earlier, the inner loop will execute while both the outer and inner loop expression are true, giving the output above.