|
1 | 1 | --- |
2 | | -title: "if-else Statement (C++)" |
3 | | -description: "The if-else, if-else with initializer, and if-constexpr statements in the C++ programming language." |
| 2 | +title: "if-else statement (C++)" |
| 3 | +description: "Use if-else, if-else with initializer, and if-constexpr statements to control conditional branching." |
4 | 4 | ms.date: 10/02/2020 |
5 | | -description: "Use if-else statements in C++ to control conditional branching." |
6 | 5 | f1_keywords: ["else_cpp", "if_cpp"] |
7 | 6 | helpviewer_keywords: ["if keyword [C++]", "else keyword [C++]"] |
8 | 7 | ms.assetid: f8c45cde-6bce-42ae-81db-426b3dbd4caa |
9 | 8 | --- |
10 | | -# if-else Statement (C++) |
| 9 | +# if-else statement (C++) |
11 | 10 |
|
12 | 11 | An if-else statement controls conditional branching. Statements in the *`if-branch`* are executed only if the *`condition`* evaluates to a non-zero 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. |
13 | 12 |
|
@@ -52,9 +51,15 @@ An if-else statement controls conditional branching. Statements in the *`if-bran |
52 | 51 |   **`if`** **`constexpr`**<sub>*opt*</sub><sup>17</sup> **`(`** *`init-statement`*<sub>*opt*</sub><sup>17</sup> *`condition`* **`)`** *`if-branch`*\ |
53 | 52 |   **`if`** **`constexpr`**<sub>*opt*</sub><sup>17</sup> **`(`** *`init-statement`*<sub>*opt*</sub><sup>17</sup> *`condition`* **`)`** *`if-branch`* **`else`** *`else-branch`* |
54 | 53 |
|
55 | | -<sup>17</sup> This element is available starting in C++17. |
| 54 | +<sup>17</sup> This optional element is available starting in C++17. |
56 | 55 |
|
57 | | -## Example if-else statements |
| 56 | +## if-else statements |
| 57 | + |
| 58 | +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). |
| 59 | + |
| 60 | +The **`else`** clause of an `if...else` statement is associated with the closest previous **`if`** statement in the same scope that does not have a corresponding **`else`** statement. |
| 61 | + |
| 62 | +### Example |
58 | 63 |
|
59 | 64 | This sample code shows several **`if`** statements in use, both with and without **`else`**: |
60 | 65 |
|
@@ -151,10 +156,6 @@ int main() |
151 | 156 | } |
152 | 157 | ``` |
153 | 158 |
|
154 | | -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). |
155 | | - |
156 | | -The **`else`** clause of an `if...else` statement is associated with the closest previous **`if`** statement in the same scope that does not have a corresponding **`else`** statement. |
157 | | - |
158 | 159 | ## <a name="if_constexpr"> if constexpr statements |
159 | 160 |
|
160 | 161 | Starting in C++17, you can use an **`if constexpr`** statement in function templates to make compile-time branching decisions without having to resort to multiple function overloads. **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. |
|
0 commit comments