You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
10
+
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.
11
11
12
-
*`condition`* expressions that evaluate to non-zero are:
12
+
*`condition`* expressions that evaluate to nonzero are:
13
13
14
14
-**`true`**
15
15
- a non-null pointer,
16
-
- any non-zero arithmetic value, or
16
+
- any nonzero arithmetic value, or
17
17
- 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).)
18
18
19
19
## Syntax
@@ -109,7 +109,7 @@ x < 11 is true!
109
109
110
110
## <aname="if_with_init"></a> if statement with an initializer
111
111
112
-
Starting in C++17, an **`if`** statement may 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.
112
+
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.
113
113
114
114
### Example
115
115
@@ -201,9 +201,9 @@ int main()
201
201
}
202
202
```
203
203
204
-
The **`if constexpr`** statement is evaluated at compile time, and the compiler only generates code for the **`if`** branch that matches the type of the argument sent to the function template. If you comment out the **`if constexpr`** statement and uncomment the **`if`** statement, the compiler generates code for both branches. That means you'll get an error:
205
-
- If you call `ShowValue(a);` you'll get an error on `return *t` because `t` is not a pointer, even though the **`if`** statement is false and the code is never executed.
206
-
- If you call `ShowValue(pB);` you'll get an error on `return t` because `t` is a pointer, even though the **`if`** statement is true and the code is never executed.
204
+
The **`if constexpr`** statement is evaluated at compile time, and the compiler only generates code for the **`if`** branch that matches the type of the argument sent to the function template. If you comment out the **`if constexpr`** statement and uncomment the **`if`** statement, the compiler generates code for both branches. That means you get an error:
205
+
- If you call `ShowValue(a);` you get an error on `return *t` because `t` isn't a pointer, even though the **`if`** statement is false and the code is never executed.
206
+
- If you call `ShowValue(pB);` you get an error on `return t` because `t` is a pointer, even though the **`if`** statement is true and the code is never executed.
207
207
208
208
Using `if constexpr` solves this problem because only the statement that matches the type of the argument sent to the function template is compiled.
0 commit comments