Here is the output of this code:
10
9 8 7 6 5 4 3 2 1
Blast off!
Warning
If the last expression in the for parentheses decrements in some way, the initial value
must be greater than the test value for the loop to execute. In the previous for
statement, the initial value of 10 is greater than the testExpression's 0
comparison.
You also do not have to increase or decrease your loop counter by 1. The following for loop counts
up by threes, beginning with 1 :
Click here to view code image
for (i = 1; i < 18; i += 3)
{
printf("%d ", i); // Prints 1, 4, 7, 10, 13, 16
}
The following code produces an interesting effect:
Click here to view code image
for (outer = 1; outer <= 3; outer++)
{
for (inner = 1; inner <= 5; inner++)
{
printf("%d ", inner)
}
// Print a newline when each inner loop finishes
printf("\n");
}
Here is the code’s output:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
If you put a for loop in the body of another loop, you are nesting the loops. In effect, the inner loop
executes as many times as the outer loop dictates. You might need a nested for loop if you wanted to