The For Statement

Java isn’t as redundant as perl, but there’s still almost always more than one way to write any given program. The following program produces identical output to the Fahrenheit to Celsius program in the preceding section. The main difference is the for loop instead of a while loop.

// Print a Fahrenheit to Celsius table

class FahrToCelsius  {

  public static void main (String args[]) {

    int fahr, celsius;
    int lower, upper, step;

    lower = 0;      // lower limit of temperature table
    upper = 300;  // upper limit of temperature table
    step  = 20;     // step size

    for (fahr=lower; fahr <= upper; fahr = fahr + step) {
      celsius = 5 * (fahr-32) / 9;
      System.out.println(fahr + " " + celsius);
    } // for loop ends here

  } // main ends here

}

The only difference between this program and the previous one is that here we’ve used a for loop instead of a while loop. The for loop has identical syntax to C’s for loops. i.e.

for (initialization; test; increment) The initialization, in this case setting the variable fahr equal to the lower limit, happens the first time the loop is entered and only the first time. Then the first time and every time after that when control reaches the top of the loop a test is made. In our example the test is whether the variable fahr is less than or equal to the upper limit. If it is we execute the code in the loop one more time. If not we begin executing the code that follows the loop. Finally at the end of each loop the increment step is made. In this case we increase fahr by step.

If that’s unclear let’s look at a simpler example:

//Count to ten

class CountToTen  {

  public static void main (String args[]) {

    int i;
    for (i=1; i <=10; i = i + 1) {
      System.out.println(i);
    }
    System.out.println("All done!");

}

}

This program prints out the numbers from one to ten. It begins by setting the variable i to 1. Then it checks to see if one is in fact less than or equal to ten. Since one is less than ten, the program prints it. Finally it adds one to i and starts over. i is now 2. The program checks to see if 2 is less than 10. It is! so the program prints “2″ and adds one to i again. i is now three. Once again the code checks to see that 3 is less than or equal to 10. This is getting rather boring rather quickly. Fortunately computers don’t get bored, and very soon computer has counted to the point where i is now ten. The computer prints “10″ and adds one to ten. Now i is eleven. Eleven is not less than or equal to ten so the computer does not print it. Rather it moves to the next statement after the end of the for loop, System.out.println("All done!);. The computer prints “All Done!” and the program ends.

For loops do not always work this smoothly. For instance consider the following program:

//Count to ten??

class BuggyCountToTen  {

  public static void main (String args[]) {

    int i;
    for (i=1; i <=10; i = i - 1) {
      System.out.println(i);
    }
    System.out.println("All done!");

}

}

This program counts backwards. There’s nothing fundamentally wrong with a program counting backwards, but we’re testing for i being bigger than ten. Since i is never going to be bigger than ten in this program, the program never stops. (That’s not completely true. If you have a very fast machine or you wait long enough somewhere below negative two billion i will suddenly become a very large positive number and the program will halt. This happens because of vagaries in computer arithmetic we’ll discuss later. Nonetheless this is still almost certainly a bug. If you don’t want to wait for that to happen just type Control-C to abort the program. This sort of behavior is referred to as an infinite loop and is a more common programming error than you might think.

Comments are closed.