for tracer Ready
main.c

                

Ready

Statements run 0 Lines printed 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

How it works

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
The same loop written with for and with while
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

  1. ① The start part runs ONCE, before anything else.
  2. ② The condition is checked before every round — false means the loop is over.
  3. The body runs.
  4. ③ The step part runs after the body, then back to ②.
  5. If ② is false at the start, the body runs 0 times.

Counting patterns

  1. Up: for (i = 1; i <= n; i++)
  2. Down: for (i = n; i >= 1; i--)
  3. In twos: for (i = 0; i <= 10; i += 2)
  4. n times: for (i = 0; i < n; i++) — the most common form
  5. Sum: start at 0 and add; product: start at 1 and multiply.

Details

  1. A variable declared in the header (int i = 0) exists only inside the loop.
  2. Each part may contain several expressions with commas: i = 0, j = 10.
  3. Every part may be left out; for (;;) loops for ever.
  4. Do not change the loop variable inside the body as well — it confuses the count.
  5. Separate the parts with semicolons, not commas.
Walkthrough

A multiplication table and powers of 2

Every line, explained.

1 #include <stdio.h>
2
3 int main(void)
4 {
5 int n = 7;
6 for (int i = 1; i <= 10; i++) i takes 1 … 10: ten rounds.
7 printf("%d x %d = %d\n", n, i, n * i); Prints 7 x 1 = 7 … 7 x 10 = 70.
8 int power = 1; A running product starts at 1.
9 for (int k = 0; k < 8; k++) { k takes 0 … 7: eight rounds (k < 8 is the usual "n times" form).
10 printf("2^%d = %d\n", k, power); Prints 2^0 = 1 … 2^7 = 128.
11 power *= 2; Double it for the next round.
12 }
13 return 0;
14 }
Common mistakes

Spot the bug

Mistakes almost every beginner makes — and how to fix them.

A semicolon after for ( … )

✗ Wrong
for (i = 0; i < 3; i++);
    printf("%d\n", i);
✓ Right
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

✗ Wrong
for (i = 0, i < 5, i++)
✓ Right
for (i = 0; i < 5; i++)

The three parts are separated by semicolons. With commas the compiler says: expected ';' before ')'.

One round too many

✗ Wrong
// print "Hi" 10 times
for (i = 0; i <= 10; i++)
✓ Right
// 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

✗ Wrong
for (int i = 0; i < 3; i++) { … }
printf("%d", i);
✓ Right
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

✗ Wrong
for (i = 0; i < 10; i++) {
    printf("%d ", i);
    i++;
}
✓ Right
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 ③.

Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 5 correct
Question 1

for (int i = 0; i < 4; i++) printf("%d", i); — output?

Question 2

How many times does the start part ① of a for run?

Question 3

for (int i = 10; i > 0; i -= 3) printf("%d ", i); — output?

Question 4

Which loop runs exactly 5 times?

Question 5

int s = 0; for (int i = 1; i <= 4; i++) s += i; — s is?

Assessment

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.