Chapter 3 - Java for Beginners Course

While and Do-While Statements

The while and do-while are looping statements that execute a given block of code while a given boolean expression is true.

while statement

The while statement has the following syntax:

while(boolean expression is true) {
	// execute this block of code
}

If the boolean expression, or loop condition, evaluates to true, then the block of code will be executed. Once the execution reaches the last line of the block of code, the execution will loop back to the starting point of the while loop and will evaluate the boolean expression once again, repeating the same process.

If the boolean expression is false, then the loop stops and the execution continues after the block of code of the while loop.

With the while statement, the block of code might not be executed at all if the loop condition evaluates to false the first time. This means, the block of code will be executed 0 or more times.

As an example, the following code sums all the numbers from 1 to the value given in the n variable:

int n = 3;
int sum = 0;
int count = 1;
while (count <= n) {
    sum = sum + count;
    count++;
}

System.out.println("The sum from 1 to " + n + " is: " + sum);

Output:

The sum from 1 to 3 is: 6
Try running the example application with different values of n. What happens if you give n a value of 0 or a negative value?

Analysing the while loop example

The following table represents what happens in the execution of the example above. The first row of the table illustrates what happens the first time the while loop is executed (first iteration), the 2nd row the 2nd time (second iteration), etc.

Iteration Loop Condition (count <= n) Is Block Of Code Executed? New value of sum New value of count

1

(1 <= 3) = true

Yes

0 + 1 = 1

2

2

(2 <= 3) = true

Yes

1 + 2 = 3

3

3

(3 <= 3) = true

Yes

3 + 3 = 6

4

4

(4 <= 3) = false

No

 

 

To give more details about the table, in the first iteration the loop condition checks if the count variable is less than or equal to n, which is true, 1 is less than 3. As the condition is true, the block of code is executed.

Inside of the loop, we update our sum variable, by adding the current value of sum (in this case 0) to the value of count (1 in this iteration), giving a result of 1.

Finally, our count variable is incremented with a new value of 2.

The same process is followed for the next iterations until we reach the point where count is assigned a value of 4. This causes the loop condition to be false (4 is not less than or equal to n that has a value of 3)

do-while statement

The do-while statement has the following syntax:

do {
	// execute this block of code
} while(boolean expression is true);

It performs the same job as the while loop, the difference relies on when the loop condition is evaluated. In the case of the do-while the loop condition is evaluated at the end of the block of code. As a result, the block of code will be executed at least once.

For example, the following code calculates the factorial of the m variable. The factorial of a number (m!) equals 1 * 2 * …​ * m.

int m = 4;
int factorial = 1;
int current = 1;
do {
    factorial = factorial * current;
    current++;
} while (current <= m);

System.out.println("The factorial of " + m + " is: " + factorial);

Output:

The factorial of 4 is: 24
Try running the example application with different values for m and see what results you get. What happens if you assign a negative value?