The first type of loop is called a while loop. Its name suits its function: to do something over and over again while a condition remains true. Here’s what a while loop looks like:

while (condition) {      
 // a block of commands (the loop body)      

The operation of a while loop is best illustrated by the flowchart in Figure 1.

Figure 1: A while loop

In most cases, the condition for a while loop will be something that starts off being true, but eventually will change to false after the block of commands has been executed a certain number of times. Here’s a simple program that uses a while loop:

1  /**      
2   * Countdown.java      
3   * A simple Java program that counts down from 10.      
4   */      
6  class Countdown {      
7    public static void main(String[] args) {      
8      int count = 10;      
10     System.out.println("Countdown to liftoff...");      
11     while (count > 0) {      
12       System.out.println(count + "!");      
13       count = count - 1;      
14     }      
15     System.out.println("Liftoff! We have liftoff!");      
16   }      
17 }

Here’s the breakdown of this program:

  • Line 8: We declare a variable called count with a value of 10.
  • Line 11: A while loop that will continue for as long as count is greater than 0.
  • Line 13: Subtract 1 from the value stored in count.

So when the program first gets to the while loop, count is 10 so the loop body is executed, printing out "10!" and then subtracting 1 from the value stored in count. The condition is then checked again. 9 is still greater than zero, so the loop body is executed once more ("9!"). This continues until the 10th time the loop body is executed ("1!"), when subtracting 1 from count brings its value to zero. Now when the condition is checked it is false; thus, the loop stops and execution continues following the loop.

Here’s what you should see when you compile and run this program:

D:javaJavaLanguageBasics> java Countdown      
Countdown to liftoff...      
Liftoff! We have liftoff!
while loops are very handy, but there are two limiting cases you should be aware of:

  • If the condition is false to begin with, the loop body won't be executed at all.
  • If the condition stays true forever, the program will execute the loop over and over and will never stop.

While the first case has its uses, the second case is something to be careful of. A loop that never ends is called an infinite loop. Infinite loops can happen under the most unexpected circumstances, and are a surprisingly common mistake that even experienced programmers sometimes make. If one of your programs ever seems to get 'stuck' and just stops responding, chances are good that it's gotten stuck in an infinite loop. You can regain control by pressing Ctrl-C, which will interrupt the program, forcing it to stop. Of course, an infinite loop in a program like the one above would be fairly obvious, since the println statement within the loop would cause the program to print out an endless stream of numbers. To see what this would look like, just change the condition for the while loop on line 11:

11     while (true) {

Don’t worry; it won’t hurt your computer. It might make you a little dizzy, though!

Go to page: 1 | 2 | 3 | 4 | 5 | 6 | 7
Kevin began developing for the Web in 1995 and is a highly respected technical author. Kev is a world-renowned author, speaker and JavaScript expert. He has a passion for making web technology easy to understand by anyone. Yes, even you!

No Reader comments

Ending Soon
Free SitePoint Premium

Get one free year of unlimited book and course downloads on SitePoint Premium!