Control Flow
Range For
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
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;
}
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;for (int value : values)
pass 1 of 49for (int value3 : values⟨addr A⟩) {10 if (value >= threshold) {All 4 passes — pass 1 is the card above pass valuethresholdcount1 3 — — 2 5 — — 3 7 6 0 → 1 4 9 6 1 → 2 count ← 1
pass 1 of 29for (int value : values) {10 if (value7 >= threshold6) {11 count→ 1++;12 }count ← 2
pass 2 of 29for (int value : values) {10 if (value9 >= threshold6) {11 count→ 2++;12 }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
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;for (int value : values)
pass 1 of 49for (int value3 : values⟨addr A⟩) {10 if (value >= threshold) {All 4 passes — pass 1 is the card above pass value1 3 2 5 3 7 4 9 count ← 1
pass 1 of 39for (int value : values) {10 if (value5 >= threshold4) {11 count→ 1++;12 }All 3 passes — pass 1 is the card above pass valuecount1 5 0 → 1 2 7 1 → 2 3 9 2 → 3 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
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;for (int value : values)
pass 1 of 49for (int value3 : values⟨addr A⟩) {10 if (value >= threshold) {All 4 passes — pass 1 is the card above pass valuethresholdcount1 3 — — 2 5 — — 3 7 — — 4 9 8 0 → 1 count ← 1
9for (int value : values) {10 if (value9 >= threshold8) {11 count→ 1++;12 }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
valuesstores3,5,7, and9.thresholdis6.- The range loop visits each value directly.
- Values at least
6increasecount. | 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