Completion requirements
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.
3. For Statement
Answer:
Yes, it certainly would.
For Statement
for ( initialize ; test ; change )
loopBody ;
The initialize, test , and change are expressions that (usually) perform the named action. The loopBody can be a single statement or a block statement.
Here is an example of a for
statement:
for ( count = 0; count < 10; count++ )
System.out.print( count + " " );
Remember that count++
has the same effect as count = count + 1
.
The for
loop does the same thing as this loop built using a while
statement:
count = 0; // initialize
while ( count < 10 ) // test
{
System.out.print( count + " ");
count++ ; // change
}
The variable count
in both loops is the loop control variable. (A loop control variable is an ordinary variable used to control the actions of a looping structure.)
Question 3:
What is the output of both loops?