Conditionally executes another statement.
Used where code needs to be executed based on a condition, or whether the if statement is evaluated in a manifestly constant-evaluated context(since C++23).
Syntax
attr (optional) if constexpr(optional)( init-statement (optional) condition ) statement-true
|
(1) | ||||||||
attr (optional) if constexpr(optional)( init-statement (optional) condition ) statement-true else statement-false
|
(2) | ||||||||
attr (optional) if !(optional) consteval compound-statement
|
(3) | (since C++23) | |||||||
attr (optional) if !(optional) consteval compound-statement else statement
|
(4) | (since C++23) | |||||||
| attr | - | (since C++11) any number of attributes | ||
constexpr
|
- | (since C++17) if present, the statement becomes a constexpr if statement | ||
| init-statement | - | (since C++17) either
Note that any init-statement must end with a semicolon. This is why it is often described informally as an expression or a declaration followed by a semicolon. | ||
| condition | - | a condition | ||
| statement-true | - | the statement to be executed if condition yields true
| ||
| statement-false | - | the statement to be executed if condition yields false
| ||
| compound-statement | - | the compound statement to be executed if the if statement is evaluated in a manifestly constant-evaluated context (or is not evaluated in such a context if ! is preceding consteval)
| ||
| statement | - | the statement (must be a compound statement, see below) to be executed if the if statement is not evaluated in a manifestly constant-evaluated context (or is evaluated in such a context if ! is preceding consteval)
|
Condition
A condition can either be an expression or a simple declaration.
|
(since C++26) |
- If it can be syntactically resolved as an expression, it is treated as an expression. Otherwise, it is treated as a declaration that is not a structured binding declaration(since C++26).
When control reaches condition, the condition will yield a value, which is used to determine which branch the control will go to.
Expression
If condition is an expression, the value it yields is the the value of the expression contextually converted to bool. If that conversion is ill-formed, the program is ill-formed.
Declaration
If condition is a simple declaration, the value it yields is the value of the decision variable (see below) contextually converted to bool. If that conversion is ill-formed, the program is ill-formed.
Non-structured binding declaration
The declaration has the following restrictions:
- Syntactically conforms to the following form:
|
(until C++11) |
|
(since C++11) |
- The declarator cannot specify a function or an array.
- The type specifier sequence(until C++11)declaration specifier sequence can only contain type specifiers and constexpr, and it(since C++11) cannot define a class or enumeration.
The decision variable of the declaration is the declared variable.
Structured binding declarationThe declaration has the following restrictions:
The decision variable of the declaration is the invented variable |
(since C++26) |
Branch selection
If the condition yields true, statement-true is executed.
If the else part of the if statement is present and condition yields false, statement-false is executed.
If the else part of the if statement is present and statement-true is also an if statement, then that inner if statement must contain an else part as well (in other words, in nested if statements, the else is associated with the closest if that does not yet have an associated else).
#include <iostream>
int main()
{
// simple if-statement with an else clause
int i = 2;
if (i > 2)
std::cout << i << " is greater than 2\n";
else
std::cout << i << " is not greater than 2\n";
// nested if-statement
int j = 1;
if (i > 1)
if (j > 2)
std::cout << i << " > 1 and " << j << " > 2\n";
else // this else is part of if (j > 2), not of if (i > 1)
std::cout << i << " > 1 and " << j << " <= 2\n";
// declarations can be used as conditions with dynamic_cast
struct Base
{
virtual ~Base() {}
};
struct Derived : Base
{
void df() { std::cout << "df()\n"; }
};
Base* bp1 = new Base;
Base* bp2 = new Derived;
if (Derived* p = dynamic_cast<Derived*>(bp1)) // cast fails, returns nullptr
p->df(); // not executed
if (auto p = dynamic_cast<Derived*>(bp2)) // cast succeeds
p->df(); // executed
}2 is not greater than 2
2 > 1 and 1 <= 2
df()
if statements with initializerIf init-statement is used, the if statement is equivalent to
or
Except that names declared by the init-statement (if init-statement is a declaration) and names declared by condition (if condition is a declaration) are in the same scope, which is also the scope of both statement s. |
(since C++17) | ||||||||||||||||||||||||||||||||||||||||||||||
Constexpr ifThe statement that begins with if constexpr is known as the constexpr if statement. All substatements of a constexpr if statement are control-flow-limited statements. In a constexpr if statement, condition must be a contextually converted constant expression of type bool(until C++23)an expression contextually converted to bool, where the conversion is a constant expression(since C++23). If condition yields The return statements in a discarded statement do not participate in function return type deduction: The discarded statement can ODR-use a variable that is not defined: Outside a template, a discarded statement is fully checked. if constexpr is not a substitute for the If a constexpr if statement appears inside a templated entity, and if condition is not value-dependent after instantiation, the discarded statement is not instantiated when the enclosing template is instantiated. The condition remains value-dependent after instantiation is a nested template: The discarded statement cannot be ill-formed for every possible specialization: The common workaround before the implementation of CWG issue 2518 for such a catch-all statement is a type-dependent expression that is always A typedef declaration or alias declaration(since C++23) can be used as the init-statement of a constexpr if statement to reduce the scope of the type alias. This section is incomplete
Reason: no example |
(since C++17) |
Consteval ifThe statement that begins with if consteval is known as the consteval if statement. All substatements of a consteval if statement are control-flow-limited statements. statement must be a compound statement, and it will still be treated as a part of the consteval if statement even if it is not a compound statement (and thus results in a compilation error): If a consteval if statement is evaluated in a manifestly constant-evaluated context, compound-statement is executed. Otherwise, statement is executed if it is present. If the statement begins with if !consteval, the compound-statement and statement (if any) must both be compound statements. Such statements are not considered consteval if statements, but are equivalent to consteval if statements:
compound-statement in a consteval if statement (or statement in the negative form) is in an immediate function context, in which a call to an immediate function needs not to be a constant expression. |
(since C++23) |
Notes
If statement-true or statement-false is not a compound statement, it is treated as if it were:
if (x)
int i;
// i is no longer in scopeis the same as
if (x)
{
int i;
}
// i is no longer in scopeThe scope of the name introduced by condition, if it is a declaration, is the combined scope of both statements' bodies:
if (int x = f())
{
int x; // error: redeclaration of x
}
else
{
int x; // error: redeclaration of x
}If statement-true is entered by goto or longjmp, condition is not evaluated and statement-false is not executed.
|
Built-in conversions are not allowed in the condition of a constexpr if statement, except for non-narrowing integral conversions to bool. |
(since C++17) (until C++23) |
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_if_constexpr |
201606L |
(C++17) | constexpr if
|
__cpp_if_consteval |
202106L |
(C++23) | consteval if
|
Keywords
if,
else,
constexpr,
consteval
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| CWG 631 | C++98 | the control flow was unspecified if the first substatement is reached via a label |
the condition is not evaluated and the second substatement is not executed (same as in C) |
See also
| (C++20) |
detects whether the call occurs within a constant-evaluated context (function) |
| C documentation for if statement
| |
Content from this page on cppreference.com, licensed under CC-BY-SA 3.0 and GFDL. Modified: restyled, site navigation removed, code windows added. Offline snapshot of 9 February 2025.