The while Statement
What You Will Learn
  • How to create and use a Java while statement
  • What is a counter-controlled loop
  • What is a sentinel-controlled loop

Jeroo Review

In Jeroo, when you wanted to repeat a process you used a while statement like the example below.

while(!bob.isWater(AHEAD))
{
   bob.hop();
}

A Jeroo while statement had the following structure.

while(
condition
) {
one or more statements
}

Recall that a condition is a statement that evaluates to either true or false. A while loop repeats as long as its condition is true. One of the limitations of Jeroo is that the condition had to be formed using the Jeroo sensor methods(isJeroo, isWater, isNet, etc.)

Java while Statement

Java also has a while statement that follows the same structure as the Jeroo while loop. However, the condition of the Java while loop is not restricted to using only sensor methods which makes it much more versatile.

Java while loops can be classified into two categories: counter-controlled loops and sentinel-controlled loops.

Counter-Controlled Loop

A counter-controlled loop is a loop that repeats a predetermined number of times. The condition of this type of loop is controlled by a counter variable. A counter is a variable that keeps count of the number of times a loop is executed. Look at the following illustration which demonstrates a counter-controlled loop.

Sentinel-Controlled Loop

A sentinel-controlled loop is a loop that executes an undetermined number of times. To stop the loop, a user enters a value, called a sentinel, which causes the loop's condition to become false. Look at the following illustration which demonstrates a sentinel-controlled loop.

Infinite Loop

A common mistake that beginner programmers make with counter-controlled loops is they forget to increment the counter variable within the body of the while loop. Look at the following example.

int cntr = 0;
while(cntr < 100)
{
   System.out.println("Hello");
}

This code creates what is called an infinite loop. In the loop above the condition will always be true since variable cntr never reaches 100, therefore the string "Hello" will print indefinitely.

Here is the code again with the increment of the counter variable included.

int cntr = 0;
while(cntr < 100)
{
   System.out.println("Hello");
   
cntr++;
}

More Examples

Example 1

The applet below demonstrates the execution of a program that uses a while statement to sum the following squence of numbers:

1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10

Press the Step button to advance the program one line. Press the Reset button to start the program over again. The applet traces the variables in the program so you can see their values at each stage of the program's execution.

Example 2

A counter variable can be incremented by something other than one. In the following example the counter is incremented by 10.

int cntr = 0;
while(cntr < 100)
{
   System.out.print(cntr + " ");
   cntr += 10;
}

Example 3

A while loop's condition can be a compound statement. In the following example the condition is a compound or(||) statement.

int grade = -1;
while(grade < 0 || grade > 100)
{
   System.out.print("Enter grade --> ");
   grade = keyboard.nextInt();
}