5.0 What This Teaches
if,else if,elseswitchand fall-throughwhileanddo-whileforloop- Range-
forover collections breakandcontinue
5.1 if and else
int score = 75;
if (score >= 90) std::cout << "A\n";
else if (score >= 80) std::cout << "B\n";
else if (score >= 70) std::cout << "C\n";
else std::cout << "F\n";
bool. Any non-zero integer converts
to true, zero to false.
5.2 switch
int day = 3;
switch (day) {
case 1: std::cout << "Monday\n"; break;
case 2: std::cout << "Tuesday\n"; break;
case 3: std::cout << "Wednesday\n"; break;
default: std::cout << "other\n";
}
case must end with break or it falls through
to the next case. default handles values not matched by any case.
switch works on integer types and enum values only.
5.3 while and do-while
// while: condition checked before each iteration
int i = 0;
while (i < 5) {
std::cout << i << " ";
++i;
}
// do-while: body executes at least once
int n = 1;
do {
std::cout << n << " ";
n *= 2;
} while (n <= 16);
while when the loop may not execute at all. Use
do-while when the body must run at least once (e.g. menu prompts).
5.4 for
for (int j = 0; j < 5; ++j)
std::cout << j * j << " "; // 0 1 4 9 16
for (;;) is an infinite
loop.
5.5 Range-for
std::vector<std::string> words = {"alpha", "beta", "gamma"};
for (const std::string& w : words)
std::cout << w << " ";
// auto shorthand - same effect
for (const auto& w : words)
std::cout << w << " ";
const auto& to avoid copies. Use auto&
when you need to modify elements. Use auto (by value) only for
cheap-to-copy types like int.
5.6 break and continue
for (int k = 0; k < 10; ++k) {
if (k == 7) break; // exit the loop entirely
if (k % 2 == 0) continue; // skip even numbers
std::cout << k << " "; // prints: 1 3 5
}
break exits the innermost loop or switch.
continue skips the rest of the current iteration and moves to
the next one.
5.7 Example - All Together
// Control_Flow - if, switch, while, do-while, for, range-for, break, continue.
#include <iostream>
#include <vector>
#include <string>
int main() {
// if/else
int score = 75;
if (score >= 90) std::cout << "A\n";
else if (score >= 80) std::cout << "B\n";
else if (score >= 70) std::cout << "C\n";
else std::cout << "F\n";
// switch
int day = 3;
switch (day) {
case 3: std::cout << "Wednesday\n"; break;
default: std::cout << "other\n";
}
// while
int i = 0;
while (i < 5) { std::cout << i << " "; ++i; }
std::cout << "\n";
// for
for (int j = 0; j < 5; ++j) std::cout << j * j << " ";
std::cout << "\n";
// range-for
std::vector<std::string> words = {"alpha", "beta", "gamma"};
for (const auto& w : words) std::cout << w << " ";
std::cout << "\n";
// break/continue
for (int k = 0; k < 10; ++k) {
if (k == 7) break;
if (k % 2 == 0) continue;
std::cout << k << " ";
}
std::cout << "\n";
return 0;
}
C
Wednesday
0 1 2 3 4
0 1 4 9 16
alpha beta gamma
1 3 5
5.8 Exercise
Exercise
- Write a loop that sums the integers 1 through 100 using a
forloop and prints the result. - Use a
whileloop to print the Fibonacci sequence while values stay below 1000. - Use a range-
forover astd::vector<int>to find and print the maximum value.
5.9 Common Mistakes
Missing break in switch
switch (x) {
case 1: std::cout << "one\n"; // falls through to case 2!
case 2: std::cout << "two\n"; break;
}
break, execution continues into the next case.
Add [[fallthrough]]; to document intentional fall-through.Off-by-one in for loops
for (int i = 0; i <= v.size(); ++i) // reads one past the end
std::cout << v[i];
// correct:
for (int i = 0; i < v.size(); ++i)
Modifying a container while range-for iterates it
std::vector inside a
range-for invalidates iterators. Use an index-based
for loop when modification is needed.
5.10 Key Terms
| Term | Meaning |
|---|---|
| if / else | Conditional branch; executes one of two blocks |
| switch | Multi-way branch on an integer or enum value |
| fall-through | Execution continues into the next case when break is absent |
| while | Loop that checks condition before each iteration |
| do-while | Loop that checks condition after each iteration; runs at least once |
| for | Loop with init, condition, and increment in one line |
| range-for | Iterates over every element of a container or array |
| break | Exits the innermost loop or switch immediately |
| continue | Skips the rest of the current iteration |