A while loop is a more freeform style of loop than the other common kind of loop we have in Java, the for loop. Both the for loop and while loop have a condition that's checked to see whether the loop should continue running; as long as the condition evaluates to true, the loop keeps running. However, the for loop also includes the counting variable's initialization and update. As we'll see, these two steps can be done without needing any special placement. Initialization of a counter variable can be done before the loop begins and updates can be done within the loop body at arbitrary points. This means a while loop is more powerful and has more flexibility than a for loop.
The following for loop and while loop are equivalent:
// for loop
for (int i = 0; i < 5; i++) {
System.out.println(":)");
}
// while loop
int i = 0;
while (i < 5) {
System.out.println(":)");
i++;
}
In the while loop, we've moved our variable initialization to before the loop. The incrementation is now in the while loop body, after the important print statement has ran.
Notice that there's two dangers in this for loop. One is that the int i
might not be declared before the loop. This would be a compiler error because there is a syntactical error and wouldn't allow us to make a .class file. The other error is that if we don't increment i, i will never be greater than 5 and thus the loop would run forever. Since this is not an issue with syntax but rather semantics (meaning), we call this a bug.
Something more profound about while loops is that they don't have to be used with counting variables like for loops usually are used with. Remember that conditions just have to be an expression that either evaluates to true or false. Numerical comparisons are one way of doing this, but we can also check for the state of something as a condition. For instance, consider the following string search loop:
String str = "abcdefghijklmnopqrstuvwxyz";
int i = 0;
while (str.charAt(i) != 'z') {
i++;
}
System.out.println("Found z");
While it's true we have a variable keeping track of a number, this is solely used to keep track of the position that we are on in the string. Instead, the while loop won't terminate until we find a character in the string that is 'z'. This could either be very good or very bad. In this case, it terminates eventually since we do find a z. But what if str
didn't have any z's in it? Then yes, this while loop would go on forever. We could fix this by adding an upper limit:
String str = "abcdefghijklmnopqrstuvwxyz";
int i = 0;
while (str.charAt(i) != 'z' && i < str.length()) {
i++;
}
System.out.println("Found z");
This way, we won't blow past the string length (and result in a runtime error), nor will the loop run forever (since, by definition now, it will only run to the string's length, which is of a finite size).
Thus, we can say the general form of a while loop looks like this:
while (<condition>) {
<statement>;
<statement>;
...
}
Back to top
© 2019–2024 Jeffrey Wang. All rights reserved.
In loving memory of Haskell, 19??-2001.