Program 5.5: A for Loop that Counts Backwards
for loops do not always work smoothly. For instance consider the following program:
//Count to ten??
class BuggyCountToTen {
public static void main (String args[]) {
for (int 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 the exit condition is that i is bigger than ten. Since i is never going to be bigger than ten in this program, the program never stops.
Copyright 1996 Elliotte Rusty Harold
elharo@sunsite.unc.edu
This Chapter
Examples
Home