A girl and a robot thinking together

Let’s think it through · Start with a story

A girl wants a robot to tap a drum three times. Writing the instruction three times works—but what if it needs to tap a hundred times?

Break down the process

Follow the numbered steps, then explain them in your own words
  1. First timeTap the drum
  2. Second timeTap the drum
  3. Third timeTap the drum
  4. StopEnough repetitions

A loop repeats a set of actions according to a condition.

To count repetitions, keep track of the initial value, the condition for continuing, and the update after each iteration. Counting from 1 to 3 runs once for 1, once for 2, and once for 3.

01Why do we need loops?

To print the numbers from 1 to 100, we could write 100 lines of cout—but that is tedious and easy to get wrong.

A loop lets us express “do this repeatedly” in a short piece of code.

02Your first for loop

for (int i = 1; i <= 3; i++) {
    std::cout << i << '\n';
}

Output:

1
2
3

03What do the three parts of for do?

int i = 1

Initialization. It runs once when entering the loop.

i <= 3

Continuation condition. It is checked before each iteration.

i++

Update. It runs after each loop body finishes.

04How does a for loop run, iteration by iteration?

Initialize i = 1↓Check i <= 3↓Run the loop body↓Run i++↺Check the condition again

When the condition becomes false, the loop ends and the program continues with the code after it.

05What does i++ mean?

i++ increases i by 1. For this example, you can think of it as moving the counter forward one step after each iteration.

More precisely, ++ is the increment operator. Prefix and postfix forms differ in more complex expressions, so beginners should avoid squeezing complicated operations into one expression.

06Can for only count up from 1?

No. You can design all three parts of a for loop to fit the task:

for (int i = 10; i >= 0; i -= 2) {
    std::cout << i << ' ';
}

It prints 10, 8, 6, 4, 2, 0.

07What is an infinite loop?

If the condition stays true, a loop may never finish normally.

for (int i = 1; i > 0; i++) {
    // ...
}

This particular example also involves the limits of integer values, so it is not a reliable way to write an intentional infinite loop. When an infinite loop is truly needed, a common form is:

for (;;) {
    // ...
}

What you have learned

  • A for loop expresses repeated execution.
  • A for loop commonly has initialization, a continuation condition, and an update.
  • The condition is checked before each iteration.
  • The update runs after the loop body.
  • for is useful for more than simple counting; it can express other rules, too.

Next: while: What If You Don’t Know How Many Times to Repeat?

Your turn

Starting at i = 1, tap the drum only while i < 3, and add 1 to i after each iteration. How many times will it tap?

Have an answer? Click to see the explanation

Twice, for i = 1 and i = 2. When i reaches 3, the condition is already false. The boundary operator changes the number of repetitions.