C++ Reference ยท Keywords and Statements
The if and else statements
Reference for C++ if and else: conditions, the init-statement form, if constexpr, if consteval, dangling else and common mistakes.
if runs a statement only when a condition is true. An optional else branch runs when the condition is false. The condition is contextually converted to bool, so integers, pointers, smart pointers and objects with an explicit operator bool can all be used directly.
Modern C++ adds three refinements. An init-statement (C++17) declares a variable that lives only inside the if/else chain. if constexpr (C++17) discards the untaken branch at compile time, which makes it a key tool in templates. if consteval (C++23) picks a branch depending on whether the code is running during constant evaluation.
if (init; condition) { // true branch } else { // false branch }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
if (condition) statement if (condition) statement else statement if (init-statement condition) statement else statement // C++17 if constexpr (constant-condition) statement else statement // C++17 if consteval compound-statement else compound-statement // C++23 if !consteval compound-statement // C++23
| Part | Description |
|---|---|
condition |
An expression or a declaration with an initializer. It is contextually converted to bool. |
init-statement |
An expression statement or simple declaration followed by ;. Names declared there are visible in both branches and destroyed at the end of the whole statement. |
constant-condition |
For if constexpr: a constant expression convertible to bool. It is evaluated at compile time. |
statement |
Any statement, usually a braced block. |
Usage notes
- Always use braces. A braceless
ifmakes it easy to add a second line that is not actually guarded. elsebinds to the nearest unmatchedif(the "dangling else"). Braces remove any doubt about whichifanelsebelongs to.- A condition may declare a variable:
if (auto p = find(); p). The variable is in scope in theelsebranch too. - Assignment inside a condition (
if (x = 5)) compiles and is almost always a typo for==. Compilers warn with-Wall. - With
if constexpr, the discarded branch is not instantiated inside a template, so it may use operations that are invalid for the current type. Outside a template the discarded branch is still fully checked. A condition that is not value-dependent gives you no such freedom. - A
returnin the discarded branch of anif constexprdoes not take part in return type deduction. if constevalrequires braces and cannot take a condition.if !consteval(orif not consteval) runs its block only at run time. A plainif (std::is_constant_evaluated())is the older equivalent but cannot call immediate functions in the true branch.- When both branches only produce a value, the conditional operator
?:can be shorter. Useifwhen the branches have side effects. - Branches of a chain that compare the same integer or enum with constants are often clearer as a
switch.
Related
- switch
- for
- constexpr
- Conditional operator
- Tutorial: if and else, switch