- 4.1 Introduction
- 4.2 Essentials of Counter-Controlled Iteration
- 4.3 for Iteration Statement
- 4.4 Examples Using the for Statement
- 4.5 Application: Summing Even Integers
- 4.6 Application: Compound-Interest Calculations
- 4.7 do... while Iteration Statement
- 4.8 switch Multiple-Selection Statement
- 4.9 C++17 Selection Statements with Initializers
- 4.10 break and continue Statements
- 4.11 Logical Operators
- 4.12 Confusing the Equality (==) and Assignment (=) Operators
- 4.13 Objects-Natural Case Study: Using the miniz-cpp Library to Write and Read ZIP files
- 4.14 C++20 Text Formatting with Field Widths and Precisions
- 4.15 Wrap-Up
17 4.9 C++17 Selection Statements with Initializers
17 Earlier, we introduced the for iteration statement. In the for header’s initialization section, we declared and initialized a control variable, which limited that variable’s scope to the for statement. C++17’s selection statements with initializers enable you to include variable initializers before the condition in an if or if…else statement and before the controlling expression of a switch statement. As with the for statement, these variables are known only in the statements where they’re declared. Figure 4.7 shows if…else statements with initializers. We’ll use both if…else and switch statements with initializers in Fig. 5.5, which implements a popular casino dice game.
1 // fig04_07.cpp 2 // C++17 if statements with initializers. 3 #include <iostream> 4 using namespace std; 5 6 int main() { 7 if (int value{7}; value == 7) { 8 cout << "value is " << value << "\n"; 9 } 10 else { 11 cout << "value is not 7; it is " << value << "\n"; 12 } 13 14 if (int value{13}; value == 9) { 15 cout << "value is " << value << "\n"; 16 } 17 else { 18 cout << "value is not 9; it is " << value << "\n"; 19 } 20 }
value is 7 value is not 9; it is 13
Fig. 4.7 C++17 if statements with initializers.
Syntax of Selection Statements with Initializers
For an if or if…else statement, you place the initializer first in the condition’s parentheses. For a switch statement, you place the initializer first in the controlling expression’s parentheses. The initializer must end with a semicolon (;), as in lines 7 and 14. The initializer can declare multiple variables of the same type in a comma-separated list.
Scope of Variables Declared in the Initializer
Any variable declared in the initializer of an if, if…else or switch statement may be used throughout the remainder of the statement. In lines 7–12, we use the variable value to determine which branch of the if…else statement to execute, then use value in the output statements of both branches. When the if…else statement terminates, value no longer exists, so we can use that identifier again in the second if…else statement to declare a new variable known only in that statement.
To prove that value is not accessible outside the if…else statements, we provided a second version of this program (fig04_07_with_error.cpp) that attempts to access variable value after (and thus outside the scope of) the second if…else statement. This produces the following compilation errors in our three compilers:
Visual Studio: 'value': undeclared identifier
Xcode: error: use of undeclared identifier 'value'
GNU g++: error: 'value' was not declared in this scope