Skip to content
  • Control Flow · C++

Control Flow

LearnViaNotes @learnvianotes· 3 min read

if, switch, for, while, do-while — plus the modern additions: range-based for, if/switch with an initializer, and [[fallthrough]].

if / else and the ternary

C++
#include <iostream>
#include <map>
#include <string>

int main() {
    int score = 72;
    if (score >= 90)      std::cout << "A\n";
    else if (score >= 70) std::cout << "B\n";
    else                  std::cout << "C\n";

    std::string parity = (score % 2 == 0) ? "even" : "odd";
    std::cout << parity << '\n';

    std::map<std::string, int> stock{{"apple", 3}};
    if (auto it = stock.find("apple"); it != stock.end())   // C++17: init; condition
        std::cout << "apples: " << it->second << '\n';      // `it` is scoped to the if/else
}
Output
B
even
apples: 3

switch

C++
#include <iostream>
void describe(char op) {
    switch (op) {
        case '+':
        case '-':
            std::cout << op << " is additive\n";
            break;
        case '*':
            std::cout << "* is multiplicative\n";
            [[fallthrough]];              // intentional: silences the warning
        case '/':
            std::cout << "  (binary operator)\n";
            break;
        default:
            std::cout << op << " unknown\n";
    }
}
int main() { describe('-'); describe('*'); describe('?'); }
Output
- is additive
* is multiplicative
  (binary operator)
? unknown

Loops

C++
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v{3, 1, 4, 1, 5};
    for (std::size_t i = 0; i < v.size(); ++i) std::cout << v[i];   // classic index loop
    std::cout << '\n';
    for (int x : v) std::cout << x * 2 << ' ';                       // range-based for
    std::cout << '\n';
    for (int& x : v) x += 10;                                        // reference: modify in place
    std::cout << v.front() << '\n';

    int n = 1;
    while (n < 100) n *= 3;
    std::cout << n << '\n';

    int tries = 0;
    do { ++tries; } while (tries < 0);   // body runs at least once
    std::cout << tries << '\n';
}
Output
31415
6 2 8 2 10
13
243
1

break, continue and nested loops

C++
#include <iostream>
int main() {
    for (int i = 1; i <= 10; ++i) {
        if (i % 2 == 0) continue;   // skip evens
        if (i > 7) break;           // stop the loop
        std::cout << i << ' ';
    }
    std::cout << '\n';

    bool found = false;              // leaving nested loops: use a flag (or a function + return)
    for (int r = 0; r < 3 && !found; ++r)
        for (int c = 0; c < 3; ++c)
            if (r * c == 2) { std::cout << r << ',' << c << '\n'; found = true; break; }
}
Output
1 3 5 7
1,2
LoopUse when
for (init; cond; step)You need the index or a custom step
for (auto& x : range)Visit every element — the default choice
while (cond)Number of iterations isn't known up front
do { } while (cond);Body must run at least once (e.g. menu loops)

How does the range-based for loop work under the hood?

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report