Logout

Looping 1 - The While Loop

Flow of control is accomplished in Java through conditional blocks, method calling, and also looping. Looping is the process of executing a series of lines of code over and over again. Looping is also referred to as iteration; to iterate is to repeat.

There are three main looping strutures, the for loop, the while loop, and the do while loop. The do while loop is not used that much, and the for loop is a little un-intuative, so it makes sense to start with the while loop. Later on, we'll look at an alternative way to loop called recursion (which is basically a method calling another instance of itself).

The While Loop Structure

boolean continueLooping = true;
while(continueLooping){ System.out.println("Loop this!");
System.out.println("Are you finished? true/false");
continueLooping = Boolean.parseBoolean(br.readLine());
}

Output would be something like the following, depending on how many times you typed in false:

Loop this!
Are you finished? true/false
false
Loop this!
Are you finished? true/false
false
Loop this!
Are you finished? true/false
true

The first thing to notice is the while line, which is very much like the if line of an if block. Following both if and in this case while is a condition, and if the condition evaluates to true, the block of code is entered. Often the condition is compound and even complex, but in the example above, it's a simple boolean variable, true or false.

(In fact, you could draw a similarity between the way methods, if blocks, and while blocks work. It's not exactly the same, but you can think of ifs and whiles as being methods that take in parameters in the parentheses that immediately follow them. What is in the parentheses in all three cases is information which is to be used inside the block. And this information is are variable, depending on what the variable is, or if it is more complex than a single variable, what the expressions evaluate to.

The other thing to particularly notice, then, is the role of the continue variable throughout the loop (and just before it), in our example. This variable controls the way the while loop works; it is called the Loop Control Variable.

When to Use A While Loop

Sometimes you know exactly how many times you want to loop some lines of code; in that case you should not choose the while loop structure (the for loop structure is better). Rather, a while loop is used when the number of times the loop is to occur is uncertain. In the above examample, we don't know when the user of the program is going to get bored, but sooner or later they will, and sooner or later they will type false, and so have the loop exit.

Another few examples could be: a bank ATM machine when you want to do more than one transaction, a program that gets you to enter names of player signing up for a team, and you don't know how many names there will be, and there are 15 places maximum, or a game that keeps on giving you harder and harder questions until you get three wrong. The while parts of those examples would be something like:

while(anotherTransactionRequested)...

while(anotherPlayerToAdd && placesLeft > 0)...

while(numberIncorrectQs < 3)...

Do note that the condition can be a simple boolean, or any expression, however complex, that evaluates ultimtely to true or fals.


One last thing to note is that it is possible for a while loop to not ever be entered. For example, in the full example above, if we had set continue to false, the loop never would have been entered. Or more likely, if we had prompted the user initially with a statement like "Would you like to play along with my silly loop program? true/false", and they typed in false, then the while loop would never be entered; those lines of code would just be skipped. We'll get to the do while loop another day, but basically that's the difference with it: it is always entered at least once. More later on that. For now just remember that the kind of while loop shown here is not necessarily entered at all - though usually it is.