< Previous | Contents | Next >

Using Empty Statements in for

Loops

You can use empty statements in creating your for loop, as I did in the following loop:

for ( ; count < 10; )

I used an empty statement for the initialization and action statements. Thats fine because I declared and initialized count before the loop and incremented it inside the loop body. This loop displays the same sequence of integers as the very first loop in the program. Although the loop might look odd, its perfectly legal.


Hin t

image

Different game programmers have different traditions. In the last chapter, you saw that you can create a loop that continues until it reaches an exit statement—such as a break—using while (true). Well, some programmers prefer to create these kinds of loops using a for statement that begins for (;;). Because the test expression in this loop is the empty statement, the loop will continue until it encounters some exit statement.

image

86 Chapter 3 n For Loops, Strings, and Arrays: Word Jumble