O'Reilly logo

Java, A Beginner’s Guide, 5th Edition by Herbert Schildt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Loops with No Body

In Java, the body associated with a for loop (or any other loop) can be empty. This is because 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:

Image

Notice that the summation process is handled entirely within the for statement, and no body is needed. Pay special attention to the iteration expression:

Image

Don’t be intimidated by statements like this. They are common in professionally written Java programs and are easy to understand if ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required