7.2 - If Statements and Blocks - Learn C++
7.2 - If Statements and Blocks - Learn C++
7.2 - If Statements and Blocks - Learn C++
The first category of control flow statements we’ll talk about are the conditional statements. A conditional
statement is a statement that specifies whether some associated statement(s) should be executed or not.
C++ supports two basic kinds of conditionals: if statements (which we introduced in lesson 4.10 -- Introduction to
if statements (https://www.learncpp.com/cpp-tutorial/introduction-to-if-statements/)2, and will talk about further here) and switch
statements (which we’ll cover in a couple of lessons).
if (condition)
true_statement;
if (condition)
true_statement;
else
false_statement;
If the condition evaluates to true , the true_statement executes. If the condition evaluates to false and the
optional else statement exists, the false_statement executes.
Here is a simple program that uses an if statement with the optional else statement :
1 #include <iostream>
3 int main()
4 {
6 int x{};
7 std::cin >> x;
9 if (x > 10)
11 else
13
14 return 0;
15 }
15 is greater than 10
Enter a number: 4
1 #include <iostream>
3 int main()
4 {
6 int x{};
7 std::cin >> x;
9 if (x > 140)
11 else
14
15 return 0;
16 }
Too bad!
This program doesn’t work as expected because the true_statement and false_statement can only be a single
statement. The indentation is deceiving us here -- the above program executes as if it had been written as follows:
1 #include <iostream>
3 int main()
4 {
6 int x{};
7 std::cin >> x;
9 if (x > 140)
11 else
13
15
16 return 0;
17 }
1 #include <iostream>
3 int main()
4 {
6 int x{};
7 std::cin >> x;
9 if (x > 140)
11 else
15 }
16
17 return 0;
18 }
Remember that blocks are treated as a single statement, so this now works as expected:
Too bad!
There are two reasons typically given as rationale for doing so. First, consider the following snippet:
2 purchaseBeer();
Now let’s say we’re in a hurry and modify this program to add another ability:
2 purchaseBeer();
Second, it can make programs more difficult to debug. Let’s say we have the following snippet:
2 addBeerToCart();
4 checkout();
Let’s say we suspect something is wrong with the addBeerToCart() function, so we comment it out:
2 // addBeerToCart();
4 checkout();
Neither of these problems occur if you always use blocks after an if or else statement.
The best argument for not using blocks around single statements is that adding blocks makes you able to see less of
your code at one time by spacing it out vertically, which makes your code less readable and can lead to other, more
serious mistakes.
The community seems to be more in favor of always using blocks than not, though this recommendation certainly
isn’t ubiquitous.
Best practice
Consider putting single statements associated with an if or else in blocks (particularly while you are
learning). More experienced C++ developers sometimes disregard this practice in favor of tighter vertical
spacing.
This avoids both of the above downsides mentioned above at some minor cost to readability.
Implicit blocks
If the programmer does not declare a block in the statement portion of an if statement or else statement , the
compiler will implicitly declare one. Thus:
if (condition)
true_statement;
else
false_statement;
if (condition)
true_statement;
else
false_statement;
Most of the time, this doesn’t matter. However, new programmers sometimes try to do something like this:
1 #include <iostream>
3 int main()
4 {
5 if (true)
6 int x{ 5 };
7 else
8 int x{ 6 };
11
12 return 0;
13 }
This won’t compile, with the compiler generating an error that identifier x isn’t defined. This is because the above
example is the equivalent of:
1 #include <iostream>
3 int main()
4 {
5 if (true)
6 {
7 int x{ 5 };
8 } // x destroyed here
9 else
10 {
11 int x{ 6 };
12 } // x destroyed here
13
15
16 return 0;
17 }
In this context, it’s clearer that variable x has block scope and is destroyed at the end of the block. By the time we
get to the std::cout line, x doesn’t exist.
Next lesson
7.3 Common if statement problems
3
Previous lesson
7.1 Control flow introduction
5