Control Flow
For Loop
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
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;
}
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;total ← 6
pass 1 of 48for (int i0 = 0; i < 4; i++) {9 total→ 6 += values[i]3 * multiplier2;10}All 4 passes — pass 1 is the card above pass ivalues[i]total1 0 3 0 → 6 2 1 5 6 → 16 3 2 7 16 → 30 4 3 9 30 → 48 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
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;total ← 3
pass 1 of 48for (int i0 = 0; i < 4; i++) {9 total→ 3 += values[i]3 * multiplier1;10}All 4 passes — pass 1 is the card above pass ivalues[i]total1 0 3 0 → 3 2 1 5 3 → 8 3 2 7 8 → 15 4 3 9 15 → 24 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
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;total ← 9
pass 1 of 48for (int i0 = 0; i < 4; i++) {9 total→ 9 += values[i]3 * multiplier3;10}All 4 passes — pass 1 is the card above pass ivalues[i]total1 0 3 0 → 9 2 1 5 9 → 24 3 2 7 24 → 45 4 3 9 45 → 72 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
valuesstores3,5,7, and9.istarts at0and stops before4.- Each pass reads
values[i]. - Each value is multiplied by
2and added tototal. | Indexi| 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