Conditionals & switch in C++

From the C++ cheat sheet · Control Flow · verified Jul 2026

Conditionals & switch

if/else and switch, including init-statements.

cpp
// Quick Reference
if (x > 0) { ... } else if (x < 0) { ... } else { ... }

switch (n) {
    case 1: ...; break;
    default: ...;
}
💡 switch needs an explicit break per case, or execution falls through to the next.
⚡ The C++17 if-init form (if (init; cond)) scopes helpers tightly to the branch.
📌 switch works on integers and enums, not on strings or floating-point values.
🟢 Stacked case labels with no break share one body - a clean way to group values.
conditionalsswitch

More C++ tasks

Back to the full C++ cheat sheet