No. In more complicated expressions it often makes a difference, but not here.
for
Parts of a for
can be omitted.
Since the three parts of a for
are the three parts of any loop,
when a part is eliminated from a for
it has to be made up elsewhere.
Recall that the for
is equivalent to a while
:
for loop | == | while loop |
---|---|---|
for ( initialize ; test ; change ) loopBody ; |
. |
initialize; while ( test ) { loopBody; change } |
If you eliminate the initialize from the for
it is
as if you eliminated it from the equivalent while
.
This is useful when initialization is complicated and you wish to
do it in several statements before the loop.
For example:
// get initial value of count from the user here for ( ; count < 13; count++ ) { System.out.println( "count is: " + count ); } System.out.println( "\nDone with the loop.\nCount is now" + count);