---
title: "if-else statement (C++)"
description: "Use if-else, if-else with initializer, and if-constexpr statements to control conditional branching."
ms.date: 10/16/2023
f1_keywords: ["else_cpp", "if_cpp"]
helpviewer_keywords: ["if keyword [C++]", "else keyword [C++]"]
---
# if-else statement (C++)
An if-else statement controls conditional branching. Statements in the *`if-branch`* are executed only if the *`condition`* evaluates to a nonzero value (or **`true`**). If the value of *`condition`* is nonzero, the following statement gets executed, and the statement following the optional **`else`** gets skipped. Otherwise, the following statement gets skipped, and if there's an **`else`** then the statement following the **`else`** gets executed.
*`condition`* expressions that evaluate to nonzero are:
- **`true`**
- a non-null pointer,
- any nonzero arithmetic value, or
- a class type that defines an unambiguous conversion to an arithmetic, boolean, or pointer type. (For information about conversions, see [Standard Conversions](../cpp/standard-conversions.md).)
## Syntax
*`init-statement`*:\
*`expression-statement`*\
*`simple-declaration`*
*`condition`*:\
*`expression`*\
*`attribute-specifier-seq`**opt* *`decl-specifier-seq`* *`declarator`* *`brace-or-equal-initializer`*
*`statement`*:\
*`expression-statement`*\
*`compound-statement`*
*`expression-statement`*:\
*`expression`**opt* **`;`**
*`compound-statement`*:\
**`{`** *`statement-seq`**opt* **`}`**
*`statement-seq`*:\
*`statement`*\
*`statement-seq`* *`statement`*
*`if-branch`*:\
*`statement`*
*`else-branch`*:\
*`statement`*
*`selection-statement`*:\
**`if`** **`constexpr`***opt*17 **`(`** *`init-statement`**opt*17 *`condition`* **`)`** *`if-branch`*\
**`if`** **`constexpr`***opt*17 **`(`** *`init-statement`**opt*17 *`condition`* **`)`** *`if-branch`* **`else`** *`else-branch`*
17 This optional element is available starting in C++17.
## if-else statements
In all forms of the **`if`** statement, *`condition`*, which can have any value except a structure, is evaluated, including all side effects. Control passes from the **`if`** statement to the next statement in the program unless the executed *`if-branch`* or *`else-branch`* contains a [`break`](../cpp/break-statement-cpp.md), [`continue`](../cpp/continue-statement-cpp.md), or [`goto`](../cpp/goto-statement-cpp.md).
The **`else`** clause of an `if...else` statement is associated with the closest previous **`if`** statement in the same scope that doesn't have a corresponding **`else`** statement.
### Example
This sample code shows several **`if`** statements in use, both with and without **`else`**:
```cpp
// if_else_statement.cpp
#include
using namespace std;
int main()
{
int x = 10;
if (x < 11)
{
cout << "x < 11 is true!\n"; // executed
}
else
{
cout << "x < 11 is false!\n"; // not executed
}
// no else statement
bool flag = false;
if (flag == true)
{
x = 100; // not executed
}
int *p = new int(25);
if (p)
{
cout << *p << "\n"; // outputs 25
}
else
{
cout << "p is null!\n"; // executed if memory allocation fails
}
}
```
Output:
```output
x < 11 is true!
25
```
## if statement with an initializer
Starting in C++17, an **`if`** statement might also contain an *`init-statement`* expression that declares and initializes a named variable. Use this form of the if-statement when the variable is only needed within the scope of the if-statement. **Microsoft-specific**: This form is available starting in Visual Studio 2017 version 15.3, and requires at least the [`/std:c++17`](../build/reference/std-specify-language-standard-version.md) compiler option.
### Example
```cpp
// Compile with /std:c++17
#include
#include
#include