Loops repeat a block while a counter changes.

for loop A `for` loop can initialize a counter, test a condition, and update the counter after each pass.

Loops

limit
loops.cpp
Replay: real traced execution (multi-file project)
#include <iostream>

int main() {
    int limit = 4;
    int total = 0;

    for (int number = 1; number <= limit; number++) {
        total += number;
    }

    std::cout << "limit=" << limit << std::endl;
    std::cout << "total=" << total << std::endl;
    return 0;
}
#include <iostream>

int main() {
    int limit = 2;
    int total = 0;

    for (int number = 1; number <= limit; number++) {
        total += number;
    }

    std::cout << "limit=" << limit << std::endl;
    std::cout << "total=" << total << std::endl;
    return 0;
}
#include <iostream>

int main() {
    int limit = 6;
    int total = 0;

    for (int number = 1; number <= limit; number++) {
        total += number;
    }

    std::cout << "limit=" << limit << std::endl;
    std::cout << "total=" << total << std::endl;
    return 0;
}
  1. limit ← 4, total ← 0

    3int main() {4    int limit→ 4 = 4; //@limit=2, 65    int total→ 0 = 0;
  2. total ← 1

    pass 1 of 4
    7for (int number1 = 1; number <= limit4; number++) {8    total→ 1 += number1;9}
    All 4 passes — pass 1 is the card above
    passnumbertotal
    110 1
    221 3
    333 6
    446 10
  3. std::cout << "limit=" << limit << std::endl;

    11    std::cout << "limit=" << limit4 << std::endl;12    std::cout << "total=" << total10 << std::endl;13    return 0;14}
    outputlimit=4
    total=10
  1. limit ← 2, total ← 0

    3int main() {4    int limit→ 2 = 2;5    int total→ 0 = 0;
  2. total ← 1

    pass 1 of 2
    7for (int number1 = 1; number <= limit2; number++) {8    total→ 1 += number1;9}
  3. total ← 3

    pass 2 of 2
    7for (int number2 = 1; number <= limit2; number++) {8    total→ 3 += number2;9}
  4. std::cout << "limit=" << limit << std::endl;

    11    std::cout << "limit=" << limit2 << std::endl;12    std::cout << "total=" << total3 << std::endl;13    return 0;14}
    outputlimit=2
    total=3
  1. limit ← 6, total ← 0

    3int main() {4    int limit→ 6 = 6;5    int total→ 0 = 0;
  2. total ← 1

    pass 1 of 6
    7for (int number1 = 1; number <= limit6; number++) {8    total→ 1 += number1;9}
    All 6 passes — pass 1 is the card above
    passnumbertotal
    110 1
    221 3
    333 6
    446 10
    5510 15
    6615 21
  3. std::cout << "limit=" << limit << std::endl;

    11    std::cout << "limit=" << limit6 << std::endl;12    std::cout << "total=" << total21 << std::endl;13    return 0;14}
    outputlimit=6
    total=21

Follow the Loop

  1. limit starts at 4.
  2. total starts at 0.
  3. The loop adds 1, then 2, then 3, then 4.
  4. total becomes 10.
  5. The program prints limit=4 and total=10. | limit | numbers added | total | | --- | --- | --- | | 2 | 1, 2 | 3 | | 4 | 1, 2, 3, 4 | 10 | | 6 | 1, 2, 3, 4, 5, 6 | 21 |

Exercise: loops.cpp

Reproduce total=10 for limit 4, then try limit 2 and 6 and predict each total before running it.