The Syntax of the While Statement

The while and do-while statements, like the for statement, is an iterator, which means so long as a condition is met the statements in the block will continue to execute. They are commonly referred to as control flow statements or loops. As there is a slight difference between the while and do-while loop we will first look at the syntax for the while loop.

    //code to be executed
    //so long as the condition
    //is true.

The condition in this case is a boolean which means it must evaluate to either true or false. When the condition is true the loop continues to run, and when the condition becomes false the loop ends.

Here is an example:

class WhileLoop {

    public static void main(String[] args){

        int number = 1;

        while( number < 5 ){      

            System.out.println("The number of times the loop has run is " + number);


the output for this code is:

    The number of times the loop has run is 1
    The number of times the loop has run is 2
    The number of times the loop has run is 3
    The number of times the loop has run is 4

The int variable number is assigned the value of one. Then the while loop considers its condition. The value of number is less than five so the while loop executes the code within its block. The printed line is output and then number is augmented by one. The loop continues until number is no longer less than five.



    number = number + 1;

To be able to exit a loop there must be a condition that will at some point evaluate to false. This is why we add on to number. Every time the loop runs through, number is increased, and eventually, once the condition is false, the loop stops and code following the loop can be executed.

The do-while Statement

Similar to the while loop is the do-while loop which is slightly different syntactically yet a very important difference when it comes to the code execution. The basic structure of the do-while loop can be written as follows:


    //code to be executed

} while(condition);

As you can see there is a slight difference to the way a do-while loop is written as opposed to a while loop. The most important detail to remember when referring to the difference between the two iterators is in the execution of the code. The do-while loop will always run through its block statements once before evaluating whether or not the condition is true.

We could write this piece of code and it would run through once even though the condition in the while expression is false.

public class DoWhileLoop {

    public static void main(String[] args) {

        int number = 10;


            System.out.println("The condition is false but this sentance is printed once.");

        }while(number < 5);

This code prints out the sentence in the do block once before it reads the while condition.

We would use the do-while loop over the while loop when we want whatever statements are in the block to run at least once.

The following is a crude example of code that displays the Fibonaci sequence using a do-while loop:

public class DoWhileLoop {

    public static void main(String[] args) {

        int fibonaci = 1;

        int sequence = 1;

        int numbers = 0 ;

        System.out.print(1 + " ");


            numbers = fibonaci;

            fibonaci += sequence;

            sequence = numbers;

        }while(fibonaci <= 144);

The output for the ollowing code is:

1 1 2 3 5 8 13 21 34 55 89 144

The while and do-while loops are simpler iterators to construct than the for loop but each has its own unique qualities. When deciding which control flow statement to use, consider the program you are writing and think about which will work most efficiently within your code.


blog comments powered by Disqus