Conditions and loops

if / else, switch, for, the range-based for, and while.

Runs in Zerethon IDE · C++ (GCC 14) Official C++ documentation ↗
main.cpp
#include <iostream>
#include <vector>

int main() {
    for (int n = 1; n <= 4; n++) {
        if (n % 2 == 0) {
            std::cout << n << " even\n";
        } else {
            std::cout << n << " odd\n";
        }
    }

    std::vector<std::string> fruits{"apple", "pear"};
    for (const auto& fruit : fruits) std::cout << fruit << '\n';

    int i = 3;
    while (i > 0) {
        --i;
        if (i == 1) continue;
        std::cout << "i = " << i << '\n';
    }
}

Output

1 odd
2 even
3 odd
4 even
apple
pear
i = 2
i = 0

Tip: for (const auto& x : container) reads each element without copying it; drop const to change elements in place.