A for loop is useful when code needs an index variable.

indexed loop An indexed loop can use `i` to read each array element by position.

For Loop

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

int main() {
    int values[4] = {3, 5, 7, 9};
    int multiplier = 2;
    int total = 0;

    for (int i = 0; i < 4; i++) {
        total += values[i] * multiplier;
    }

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

int main() {
    int values[4] = {3, 5, 7, 9};
    int multiplier = 1;
    int total = 0;

    for (int i = 0; i < 4; i++) {
        total += values[i] * multiplier;
    }

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

int main() {
    int values[4] = {3, 5, 7, 9};
    int multiplier = 3;
    int total = 0;

    for (int i = 0; i < 4; i++) {
        total += values[i] * multiplier;
    }

    std::cout << "multiplier=" << multiplier << std::endl;
    std::cout << "total=" << total << std::endl;
    return 0;
}
  1. values ← ⟨addr A⟩, multiplier ← 2, total ← 0

    3int main() {4    int values→ ⟨addr A⟩[4] = {3, 5, 7, 9};5    int multiplier→ 2 = 2; //@multiplier=1, 36    int total→ 0 = 0;
  2. total ← 6

    pass 1 of 4
    8for (int i0 = 0; i < 4; i++) {9    total→ 6 += values[i]3 * multiplier2;10}
    All 4 passes — pass 1 is the card above
    passivalues[i]total
    1030 6
    2156 16
    32716 30
    43930 48
  3. std::cout << "multiplier=" << multiplier << std::endl;

    12    std::cout << "multiplier=" << multiplier2 << std::endl;13    std::cout << "total=" << total48 << std::endl;14    return 0;15}
    outputmultiplier=2
    total=48
  1. values ← ⟨addr A⟩, multiplier ← 1, total ← 0

    3int main() {4    int values→ ⟨addr A⟩[4] = {3, 5, 7, 9};5    int multiplier→ 1 = 1;6    int total→ 0 = 0;
  2. total ← 3

    pass 1 of 4
    8for (int i0 = 0; i < 4; i++) {9    total→ 3 += values[i]3 * multiplier1;10}
    All 4 passes — pass 1 is the card above
    passivalues[i]total
    1030 3
    2153 8
    3278 15
    43915 24
  3. std::cout << "multiplier=" << multiplier << std::endl;

    12    std::cout << "multiplier=" << multiplier1 << std::endl;13    std::cout << "total=" << total24 << std::endl;14    return 0;15}
    outputmultiplier=1
    total=24
  1. values ← ⟨addr A⟩, multiplier ← 3, total ← 0

    3int main() {4    int values→ ⟨addr A⟩[4] = {3, 5, 7, 9};5    int multiplier→ 3 = 3;6    int total→ 0 = 0;
  2. total ← 9

    pass 1 of 4
    8for (int i0 = 0; i < 4; i++) {9    total→ 9 += values[i]3 * multiplier3;10}
    All 4 passes — pass 1 is the card above
    passivalues[i]total
    1030 9
    2159 24
    32724 45
    43945 72
  3. std::cout << "multiplier=" << multiplier << std::endl;

    12    std::cout << "multiplier=" << multiplier3 << std::endl;13    std::cout << "total=" << total72 << std::endl;14    return 0;15}
    outputmultiplier=3
    total=72

Walk by Index

  1. values stores 3, 5, 7, and 9.
  2. i starts at 0 and stops before 4.
  3. Each pass reads values[i].
  4. Each value is multiplied by 2 and added to total. | Index i | Value | Added | | --- | --- | --- | | 0 | 3 | 6 | | 1 | 5 | 10 | | 2 | 7 | 14 | | 3 | 9 | 18 |

Exercise: for_loop.cpp

Use an indexed for loop to multiply each array value and add the total