Sahaj.blogs
9 Nov 2023 · StudyPhase 2 · C++

C++ · Control flow

if/else, switch, the three kinds of for loop, while and do-while, written next to their Python equivalents.

The ideas are the same as in Python. The syntax uses braces and needs parentheses around conditions.

  • if (cond) { } else if (...) { } else { }
  • switch on an integer or char. Without break, execution falls through into the next case.
  • Classic for (init; cond; step).
  • Range-based for (auto x : container) is the closest thing to Python’s for x in list.
  • while (cond) checks first. do { } while (cond); always runs at least once.
#include <iostream>
#include <vector>

int main() {
    for (int n : {7, 12, 0}) {
        if (n == 0) std::cout << n << " zero\n";
        else if (n % 2 == 0) std::cout << n << " even\n";
        else std::cout << n << " odd\n";
    }

    char grade = 'B';
    switch (grade) {
        case 'A': std::cout << "excellent\n"; break;
        case 'B': std::cout << "good\n";      break;
        default:  std::cout << "keep going\n";
    }

    std::vector<int> marks{88, 79, 95};
    int sum = 0;
    for (std::size_t i = 0; i < marks.size(); ++i) sum += marks[i];
    std::cout << "sum " << sum << '\n';

    int n = 1;
    do { n *= 3; } while (n < 100);
    std::cout << "first power of 3 >= 100: " << n << '\n';
}

Output:

7 odd
12 even
0 zero
good
sum 262
first power of 3 >= 100: 243

#cpp#control-flow

Sources

  1. if statement — cppreference.com
  2. Range-based for loop — cppreference.com
  3. switch statement — cppreference.com