A range-based for loop visits each value in a collection.

range-based for Use `for (int value : values)` when the loop needs each element but not its index.

Range For

threshold
range_for.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
#include <string>

int main() {
    int threshold = 6;
    int values[4] = {3, 5, 7, 9};
    int count = 0;

    for (int value : values) {
        if (value >= threshold) {
            count++;
        }
    }

    std::cout << "threshold=" << threshold << std::endl;
    std::cout << "count=" << count << std::endl;
    return 0;
}
#include <iostream>
#include <string>

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

    for (int value : values) {
        if (value >= threshold) {
            count++;
        }
    }

    std::cout << "threshold=" << threshold << std::endl;
    std::cout << "count=" << count << std::endl;
    return 0;
}
#include <iostream>
#include <string>

int main() {
    int threshold = 8;
    int values[4] = {3, 5, 7, 9};
    int count = 0;

    for (int value : values) {
        if (value >= threshold) {
            count++;
        }
    }

    std::cout << "threshold=" << threshold << std::endl;
    std::cout << "count=" << count << std::endl;
    return 0;
}
  1. threshold ← 6, values ← ⟨addr A⟩, count ← 0

    4int main() {5    int threshold→ 6 = 6; //@threshold=4, 86    int values→ ⟨addr A⟩[4] = {3, 5, 7, 9};7    int count→ 0 = 0;
  2. for (int value : values)

    pass 1 of 4
    9for (int value3 : values⟨addr A⟩) {10    if (value >= threshold) {
    All 4 passes — pass 1 is the card above
    passvaluethresholdcount
    13
    25
    3760 1
    4961 2
  3. count ← 1

    pass 1 of 2
    9for (int value : values) {10    if (value7 >= threshold6) {11        count→ 1++;12    }
  4. count ← 2

    pass 2 of 2
    9for (int value : values) {10    if (value9 >= threshold6) {11        count→ 2++;12    }
  5. std::cout << "threshold=" << threshold << std::endl;

    15    std::cout << "threshold=" << threshold6 << std::endl;16    std::cout << "count=" << count2 << std::endl;17    return 0;18}
    outputthreshold=6
    count=2
  1. threshold ← 4, values ← ⟨addr A⟩, count ← 0

    4int main() {5    int threshold→ 4 = 4;6    int values→ ⟨addr A⟩[4] = {3, 5, 7, 9};7    int count→ 0 = 0;
  2. for (int value : values)

    pass 1 of 4
    9for (int value3 : values⟨addr A⟩) {10    if (value >= threshold) {
    All 4 passes — pass 1 is the card above
    passvalue
    13
    25
    37
    49
  3. count ← 1

    pass 1 of 3
    9for (int value : values) {10    if (value5 >= threshold4) {11        count→ 1++;12    }
    All 3 passes — pass 1 is the card above
    passvaluecount
    150 1
    271 2
    392 3
  4. std::cout << "threshold=" << threshold << std::endl;

    15    std::cout << "threshold=" << threshold4 << std::endl;16    std::cout << "count=" << count3 << std::endl;17    return 0;18}
    outputthreshold=4
    count=3
  1. threshold ← 8, values ← ⟨addr A⟩, count ← 0

    4int main() {5    int threshold→ 8 = 8;6    int values→ ⟨addr A⟩[4] = {3, 5, 7, 9};7    int count→ 0 = 0;
  2. for (int value : values)

    pass 1 of 4
    9for (int value3 : values⟨addr A⟩) {10    if (value >= threshold) {
    All 4 passes — pass 1 is the card above
    passvaluethresholdcount
    13
    25
    37
    4980 1
  3. count ← 1

    9for (int value : values) {10    if (value9 >= threshold8) {11        count→ 1++;12    }
  4. std::cout << "threshold=" << threshold << std::endl;

    15    std::cout << "threshold=" << threshold8 << std::endl;16    std::cout << "count=" << count1 << std::endl;17    return 0;18}
    outputthreshold=8
    count=1

Count Values at the Threshold

  1. values stores 3, 5, 7, and 9.
  2. threshold is 6.
  3. The range loop visits each value directly.
  4. Values at least 6 increase count. | Value | >= 6? | Count after check | | --- | --- | --- | | 3 | no | 0 | | 5 | no | 0 | | 7 | yes | 1 | | 9 | yes | 2 |

Exercise: range_for.cpp

Use a range-based for loop to count values at or above a threshold