Java Reference
In-Depth Information
This loop will run forever. Although there are some programming tasks, such as operating
system command processors, that require an infinite loop, most “infinite loops” are really
just loops with special termination requirements. Near the end of this chapter, you will see
how to halt a loop of this type. (Hint: It's done using the break statement.)
Loops with No Body
In Java, the body associated with a for loop (or any other loop) can be empty. This is be-
cause a null statement is syntactically valid. Body-less loops are often useful. For example,
the following program uses one to sum the numbers 1 through 5:
The output from the program is shown here:
Notice that the summation process is handled entirely within the for statement, and no body
is needed. Pay special attention to the iteration expression:
Don't be intimidated by statements like this. They are common in professionally written
Java programs and are easy to understand if you break them down into their parts. In other
Search WWH ::




Custom Search