Control Flow: Conditions and Loops
Branch with if and switch, repeat with for and while, including the modern range-based for.
Making decisions
C++ decision-making mirrors C — the if/else if/else chain, with conditions in parentheses and blocks in braces:
int marks = 72;
if (marks >= 90) {
cout << "Grade A" << endl;
} else if (marks >= 75) {
cout << "Grade B" << endl;
} else if (marks >= 60) {
cout << "Grade C" << endl;
} else {
cout << "Fail" << endl;
}
C++ checks each branch top to bottom and runs the first whose condition is true, then skips the rest — so order your conditions deliberately (we check 90 before 75 before 60). Even for one-line blocks, always use the braces; it prevents a whole category of bugs when you later add a second line.
switch for many fixed values
When checking one variable against several specific values, switch is cleaner than a long else if chain:
int day = 3;
switch (day) {
case 1: cout << "Monday"; break;
case 2: cout << "Tuesday"; break;
case 3: cout << "Wednesday"; break;
default: cout << "Other day";
}
The break after each case is essential — without it, execution "falls through" into the next case, a notorious source of bugs. default catches anything unmatched.
The for loop
C++'s for loop packs start, condition, and step into one line — the workhorse of counting and iteration:
for (int i = 1; i <= 5; i++) {
cout << i << " "; // 1 2 3 4 5
}
// Classic: sum 1 to 100
int total = 0;
for (int i = 1; i <= 100; i++) {
total += i;
}
cout << total; // 5050
Read the three parts in the parentheses: int i = 1 runs once at the start; i <= 5 is checked before every pass; i++ runs after each pass. Dense, but immensely flexible once it clicks.
The range-based for loop (modern C++)
Here's a lovely C++ feature C doesn't have. When you just want to visit every element of a collection, the range-based for is cleaner and less error-prone — no index, no off-by-one mistakes:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> scores = {88, 72, 95, 60};
// Range-based for: "for each score in scores"
for (int score : scores) {
cout << score << " "; // 88 72 95 60
}
// Works with strings too
string word = "Hello";
for (char c : word) {
cout << c << "-"; // H-e-l-l-o-
}
return 0;
}
Read for (int score : scores) as "for each score in scores". It's exactly like Python's for loop, and you'll reach for it constantly once you start using the STL containers in a later chapter. (We met vector here — think of it as a resizable array; much more on it soon.)
while, do-while, and loop control
A while loop repeats while its condition holds — perfect when you don't know the count in advance. do-while checks at the bottom, guaranteeing the body runs at least once:
int count = 5;
while (count > 0) {
cout << count << " ";
count--; // must move toward the exit!
}
// 5 4 3 2 1
Inside any loop, break exits immediately and continue skips to the next pass — fine control for searching and filtering:
for (int n = 1; n <= 10; n++) {
if (n == 7) break; // stop at 7
if (n % 2 == 0) continue;// skip even numbers
cout << n << " "; // 1 3 5
}
As always with while, make sure something inside eventually makes the condition false, or you'll spin forever. With control flow mastered, we can organise code into reusable functions — and C++ adds some powerful twists there.
Finished "Control Flow: Conditions and Loops"?
Mark this chapter complete so you can pick up exactly where you left off. Your progress saves locally — sign in to sync across devices.
Was this chapter clear?
