This book is now obsolete Please use CSAwesome instead.

7.2. While Loops

A while loop executes the body of the loop as long as (or while) a Boolean condition is true. When the condition is false execution continues after the body of the while loop.

Note

If the condition is false the first time you check it, the body of the loop will not execute.

../_images/WhileLoopFlow.png

Figure 1: Flow in a while loop

You can use a while loop to repeat the body of the loop a certain number of times as shown above. However, a while loop is typically used when you don’t know how many times the loop will execute. You might use it while processing a string. Google has been scanning old books and then using software to read the scanned text. But, the software can get things mixed up like using 1 for l. The following code loops through a string replacing all 1’s with l’s.

The while loop starts on line 9 in the code above. Statements 10 through 13 are the body of the loop (from the opening parenthesis on line 10 to the closing one on line 13).

Note

Java doesn’t require your code to be correctly indented (code moved to the right a few spaces) to make it clear what statements are part of the body of the loop, but it is good practice. On the free response part of the exam, the reader will use the indention when determining the meaning of your code, even if you forget the open or close curly brace.

One thing to be careful about with while loops is making sure that you don’t end up with an infinite loop. An infinite loop is one that never stops (the condition is always true).

// an infinite loop
while (true)
{
   System.out.println("This is a loop that never ends");
}

The infinite loop above is pretty obvious. But, most infinite loops are accidental. They usually occur because you forget to change the thing you are checking in the condition.

7.2.1. Tracing Variables in Loops

A really important skill to develop is the ability to trace the values of variables and how they change during each time through a loop.

Here is a complex loop. See if you can trace the code on paper to predict what it will do when you run it.

Click on the following link to step through the code above with the Java Visualizer - Click here.

You can create a table that keeps track of the variable values each time through the loop as shown below. This is very helpful on the exam. Studies have shown that students who create tables like this do much better on code tracing problems on multiple choice exams.

../_images/whileLoopTrace.png

Figure 1: A table showing the values of all of the variables each time through the loop. The 0 means before the first loop.

You can also add System.out.println(variable) to print the value of a variable. In the code below I am printing the values of all of the variables before the loop and at the end of the loop body.

Check your understanding

Mixed up programs

The following method has the correct code to return a string with all a’s removed, but the code is mixed up. Drag the blocks from the left area into the correct order in the right area. Click on the “Check Me” button to check your solution.

You have attempted of activities on this page