The For Statement

The 'for' loop is more compact than the 'while' and 'do' loops and automatically updates the loop counter at the end of each iteration. Both 'for' and 'while' loops are designed for different situations. You'll learn more about when to use each later.

14. Omitting the Test


Answer:

Yes. (Actually, a good answer would be "sounds dangerous", because it is.)

Omitting the Test

When the test part of a for is omitted it is as if the value true were put in its place. So,

for ( initialize ;   ; change )
loopBody ;

is the same as:

for ( initialize ;  true  ; change )
loopBody ;

This is done for compatibility with the language C. It should not be used in newly written programs.

Several syntactic oddities were included in Java so that it would look familiar to C programmers.


Question 14:

Could all three parts be omitted from a for?