Java Reference
In-Depth Information
iteration . For tasks where you might want to stop partway through, there are more appropriate
loops to use—for instance, the while loop , which we will introduce next. In these cases, the num-
ber of times the loop's body will be executed is less certain; it typically depends on what happens
during the iteration. This style is often called indefinite iteration, and we explore it next.
4.10
Indefinite iteration
Using a for-each loop has given us our first experience with the principle of carrying out some
actions repeatedly. The statements inside the loop body are repeated for each item in the associ-
ated collection, and the iteration stops when we reach the end of the collection. A for-each loop
provides definite iteration ; given the state of a particular collection, the loop body will be ex-
ecuted the number of times that exactly matches the size of that collection. But there are many
situations where we want to repeat some actions but we cannot predict in advance exactly how
many times that might be. A for-each loop does not help us in those cases.
Imagine, for instance, that you have lost your keys and you need to find them before you can
leave the house. Your search will model an indefinite iteration, because there will be many
different places to look, and you cannot predict in advance how many places you will have to
search before you find the keys; after all, if you could predict that, you would go straight to
where they are! So you will do something like mentally composing a list of possible places they
could be and then visit each place in turn until you find them. Once found, you want to stop
looking rather than complete the list (which would be pointless).
What we have here is an example of indefinite iteration : the (search) action will be repeated an
unpredictable number of times, until the task is complete. Scenarios similar to key searching are
common in programming situations. While we will not always be searching for something, situ-
ations in which we want to keep doing something until the repetition is no longer necessary are
frequently encountered. Indeed, they are so common that most programming languages provide at
least one—and commonly more than one—loop construct to express them. Because what we are
trying to do with these loop constructs is typically more complex than just iterating over a complete
collection from beginning to end, they require a little more effort to understand. But this effort will
be well rewarded from the greater variety of things we can achieve with them. Our focus here will
be on Java's while loop , which is similar to loops found in other programming languages.
4.10.1
The while loop
A while loop consists of a header and a body; the body is intended to be executed repeatedly.
Here is the structure of a while loop where boolean condition and loop body are pseudo-code
but all the rest is the Java syntax:
while (boolean condition) {
loop body
}
The loop is introduced with the keyword while , which is followed by a boolean condition. The
condition is ultimately what controls how many times a particular loop will iterate. The condi-
tion is evaluated when program control first reaches the loop, and it is reevaluated each time
 
 
Search WWH ::




Custom Search