Selections in C++: if-else Statements

1. Two-Way if-else Statements

An if-else statement decides which statements to execute based on whether the condition is true or false.

A one-way if statement performs an action if the specified condition is true. If the condition is false, nothing is done. But what if you want to perform an alternative action when the condition is false? You can use a two-way if statement. A two-way if-else statement specifies different actions, depending on whether the condition is true or false.

Here is the syntax for a two-way if-else statement:

if (boolean-expression)

{

statement(s)-for-the-true-case;

}

else

{

statement(s)-for-the-false-case;

}

The flowchart of the statement is shown in Figure 3.2.

Figure 3.2 An if-else statement executes statements for the true case if the
Boolean-expression evaluates to true; otherwise, statements for the false case
are executed

If the boolean-expression evaluates to true, the statement(s) for the true case is exe­cuted; otherwise, the statement(s) for the false case is executed. For example, consider the following code:

if (radius >= 0)

area = radius * radius * PI;

{

cout << “The area for the circle of radius ” <<

radius << ” is ” << area;

}

else

{

cout << “Negative radius”;

}

If radius >= 0 is true, area is computed and displayed; if it is false, the message “Negative radius” is displayed.

As usual, the braces can be omitted if they enclose only one statement. Therefore, in the preceding example, the braces enclosing the cout << “Negative radius” statement can be omitted.

Here is another example of the if-else statement. The example checks whether a number is even or odd, as follows:

if (number % 2 == 0)

cout << number << ” is even.”;

else

cout << number << ” is odd.”;

2. Nested if and Multi-Way if-else Statements

An if statement can be inside another if statement to form a nested if statement.

The statement in an if or if-else statement can be any legal C++ statement, including another if or if-else statement. The inner if statement is said to be nested inside the outer if statement. The inner if statement can contain another if statement; in fact, there is no limit to the depth of the nesting. For example, the following is a nested if statement:

if (i > k)

{

if (j > k) 

cout << “i and j are greater than k” << endl;

}

else

cout << “i is less than or equal to k” << endl;

The if (j > k) statement is nested inside the i f (i > k) statement.

The nested if statement can be used to implement multiple alternatives. For example, the statement given in Figure 3.3a assigns a letter grade to the variable grade according to the score, with multiple alternatives.

The execution of this if statement proceeds as shown in Figure 3.4. The first condition (score >= 90.0) is tested. If it is true, the grade is A. If it is false, the second condition (score >= 80.0) is tested. If the second condition is true, the grade is B. If that condi­tion is false, the third condition and the rest of the conditions (if necessary) are tested until a condition is met or all the conditions are false. In the latter case, the grade is F. Note that a condition is tested only when all the conditions that come before it are false.

The if statement in Figure 3.3a is equivalent to the if statement in Figure 3.3b. In fact, Figure 3.3b is the preferred coding style for multiple alternative if statements. This style, called multi-way if-else statements, avoids deep indentation and makes the program easy multi-way if statement to read.

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *