control

for (int n : ports) {
    std::cout << n << '\n';
}

Range based for is the normal loop for containers It hides the iterator and reduces off by one mistakes

if

Initializers in if statements keep variables local

if (auto it = std::ranges::find(ports, 443); it != ports.end()) {
    report(*it);
}

The iterator dies with the branch

switch

Switch on enums and integral values Cases fall through unless you break Cover every enum case and let the compiler warn on a missing one

Loops

Use while when the count is unknown Use for when it is known Avoid modifying a container while iterating it, invalidation rules are not a joke

Next

Functions and templates in 04 functions and templates

Posted