for Loop
The counting loopThe for loop is the loop you will use most. It puts the three parts every loop needs — start, condition and step — on one line, so a counting loop is easy to read. In the lab below you watch the three parts light up in the order C runs them, add and multiply with a loop, and try four variations.
- ①start — runs once
- ②condition — before every round
- ③step — after every round
Ready
Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode
The ideas behind the lab
Exactly what you watched happen, in plain steps.
Three parts in one line
A for loop has three parts in its parentheses, separated by semicolons: ① the start (runs once), ② the condition (checked before every round), ③ the step (runs after every round). The order is ① ② body ③ ② body ③ … until ② is false.
for (int i = 1; i <= 5; i++) {
// ① start ② test ③ step
printf("%d\n", i); // body
}
// order: ① ② body ③ ② body ③ … ② (false) → done
| Part | for | while |
|---|---|---|
| ① start | for (int i = 1; …; …) | int i = 1; (before the loop) |
| ② condition | for (…; i <= 5; …) | while (i <= 5) |
| ③ step | for (…; …; i++) | i++; (last line of the body) |
| Best for | a known number of rounds (counting) | an unknown number of rounds (until something happens) |
How for works
- ① The start part runs ONCE, before anything else.
- ② The condition is checked before every round — false means the loop is over.
- The body runs.
- ③ The step part runs after the body, then back to ②.
- If ② is false at the start, the body runs 0 times.
Counting patterns
- Up: for (i = 1; i <= n; i++)
- Down: for (i = n; i >= 1; i--)
- In twos: for (i = 0; i <= 10; i += 2)
- n times: for (i = 0; i < n; i++) — the most common form
- Sum: start at 0 and add; product: start at 1 and multiply.
Details
- A variable declared in the header (int i = 0) exists only inside the loop.
- Each part may contain several expressions with commas: i = 0, j = 10.
- Every part may be left out; for (;;) loops for ever.
- Do not change the loop variable inside the body as well — it confuses the count.
- Separate the parts with semicolons, not commas.
A multiplication table and powers of 2
Every line, explained.
#include <stdio.h>
int main(void)
{
int n = 7;
for (int i = 1; i <= 10; i++)
i takes 1 … 10: ten rounds.
printf("%d x %d = %d\n", n, i, n * i);
Prints 7 x 1 = 7 … 7 x 10 = 70.
int power = 1;
A running product starts at 1.
for (int k = 0; k < 8; k++) {
k takes 0 … 7: eight rounds (k < 8 is the usual "n times" form).
printf("2^%d = %d\n", k, power);
Prints 2^0 = 1 … 2^7 = 128.
power *= 2;
Double it for the next round.
}
return 0;
}
Spot the bug
Mistakes almost every beginner makes — and how to fix them.
A semicolon after for ( … )
for (i = 0; i < 3; i++);
printf("%d\n", i);
for (i = 0; i < 3; i++)
printf("%d\n", i);
The ; is the body. The loop does nothing three times, then printf runs once and prints 3.
Commas instead of semicolons
for (i = 0, i < 5, i++)
for (i = 0; i < 5; i++)
The three parts are separated by semicolons. With commas the compiler says: expected ';' before ')'.
One round too many
// print "Hi" 10 times for (i = 0; i <= 10; i++)
// print "Hi" 10 times for (i = 0; i < 10; i++)
0 to 10 inclusive is 11 values. Start at 0 with <, or start at 1 with <=.
Using the loop variable after the loop
for (int i = 0; i < 3; i++) { … }
printf("%d", i);
int i;
for (i = 0; i < 3; i++) { … }
printf("%d", i);
A variable declared in the for header only exists inside the loop: 'i' undeclared.
Changing i in the body too
for (i = 0; i < 10; i++) {
printf("%d ", i);
i++;
}
for (i = 0; i < 10; i += 2)
printf("%d ", i);
i is increased twice per round, which is easy to miss. Put the whole step in part ③.
Check your understanding
Answer each question — you will see the explanation straight away.
for (int i = 0; i < 4; i++) printf("%d", i); — output?
i takes 0, 1, 2, 3; at 4 the condition is false.
How many times does the start part ① of a for run?
Only once, before the first condition check.
for (int i = 10; i > 0; i -= 3) printf("%d ", i); — output?
10, 7, 4, 1; then -2 makes i > 0 false.
Which loop runs exactly 5 times?
0, 1, 2, 3, 4 — five values.
int s = 0; for (int i = 1; i <= 4; i++) s += i; — s is?
1 + 2 + 3 + 4 = 10.
for Loop quiz
Test yourself: 10 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.