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.

10. Counting Down



Counting Down

Recall the general form of a for:

for ( initialize ; test ; change )
loopBody ;

The change can be any statement. It can, for example, decrease the control variable, as in this example:

int count;

for ( count = 10; count >= 0; count-- )
System.out.println( "count is: " + count );
System.out.println( "\nDone with the loop.\nCount is now" + count);

The expression count-- decrements count by one.


Question 10:

Would the output of this program be any different if the expression count-- were changed to --count?