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.

8. Counting by Threes


Answer:

description
start at 1, count upward by 2's, all values less than 10
sequence
1 3 5 7 9
code
for ( count= 1 ; count < 10; count+=2 )

Counting by Threes

Here is almost the same loop as in the previous example, but now the control variable is incremented by THREE.

The expression count += 3 adds 3 to the value of count .


System.out.println( "\nDone with the loop.\nCount is now" + count);

int count;

for ( count = 0; count <= 12; count += 3 )
{
System.out.println( "count is: " + count );
}

Here is a version of this loop. Try to predict the output before you run the program. (Notice that the test is now  <= ). (Also, pay careful attention to the value  count has just outside the loop.




     


Question 8:

Now complete the for statement for each of the following sequences

start at 0, count upward by 1's, end at 7
for ( count = ; count < ; count++ )
start at 0, count upward by 2's, end at 14
for ( count = ; count < ; count += 2 )
start at 1, count upward by 2's, end at 15
for ( count = ; count < ; count += 2 )