7.3 - Common If Statement Problems - Learn C++
7.3 - Common If Statement Problems - Learn C++
1 #include <iostream>
3 int main()
4 {
6 int x{};
7 std::cin >> x;
13
14 return 0;
15 }
1 #include <iostream>
3 int main()
4 {
6 int x{};
7 std::cin >> x;
13
15 else
17
18 return 0;
19 }
The above program introduces a source of potential ambiguity called a dangling else problem. Is the else
statement in the above program matched up with the outer or inner if statement ?
The answer is that an else statement is paired up with the last unmatched if statement in the same block.
Thus, in the program above, the else is matched up with the inner if statement , as if the program had been
written like this:
1 #include <iostream>
3 int main()
4 {
6 int x{};
7 std::cin >> x;
10 {
15 }
16
17 return 0;
18 }
Enter a number: 21
21 is negative
To avoid such ambiguities when nesting if statements , it is a good idea to explicitly enclose the inner if
statement within a block. This allows us to attach an else to either if statement without ambiguity:
1 #include <iostream>
3 int main()
4 {
6 int x{};
7 std::cin >> x;
9 if (x >= 0)
10 {
11 if (x <= 20)
15 }
18
19 return 0;
20 }
The else statement within the block attaches to the inner if statement , and the else statement outside of the
block attaches to the outer if statement .
3 int main()
4 {
6 int x{};
7 std::cin >> x;
9 if (x < 0)
15
16 return 0;
17 }
Here’s another example that uses logical operators to check multiple conditions within a single if statement :
1 #include <iostream>
3 int main()
4 {
6 int x{};
7 std::cin >> x;
10 int y{};
11 std::cin >> y;
12
13 if (x > 0 && y > 0) // && is logical and -- checks if both conditions are true
17 else
19
20 return 0;
21 }
Null statements
A null statement is an expression statement that consists of just a semicolon:
1 if (x > 10)
Null statements do nothing. They are typically used when the language requires a statement to exist but the
programmer doesn’t need one. For readability, null statements are typically placed on their own lines.
We’ll see examples of intentional null statements later in this chapter, when we cover loops. Null statements
are rarely intentionally used with if statements . However, they can unintentionally cause problems for new (or
careless) programmers. Consider the following snippet:
1 if (nuclearCodesActivated());
2 blowUpTheWorld();
In the above snippet, the programmer accidentally put a semicolon on the end of the if statement (a common
mistake since semicolons end many statements). This unassuming error compiles fine, and causes the snippet to
execute as if it had been written like this:
1 if (nuclearCodesActivated())
Warning
Be careful not to “terminate” your if statement with a semicolon, otherwise your conditional statement(s) will
execute unconditionally (even if they are inside a block).
1 #include <iostream>
3 int main()
4 {
6 int x{};
7 std::cin >> x;
10 else
12
13 return 0;
14 }
This program will compile and run, but will produce the wrong result in some cases:
Enter 0 or 1: 0
You entered 1
In fact, this program will always produce the result You entered 1 . This happens because x = 0 first assigns the
value 0 to x , then evaluates to the value of x , which is now 0 , which is Boolean false . Since the conditional is
always false , the else statement always executes.