add

Monday 6 February 2017

C++ nested if-else statements with examples

Two-Way Selection
There are many programming situations in which you must choose between two alternatives. For example, if a part-time employee works overtime, the paycheck is calculated using the overtime payment formula; otherwise, the paycheck is calculated using the regular formula. This is an example of two-way selection. To choose between two alternatives—that is, to implement two-way selections—C++ provides the if. . .else statement. Two-way selection uses the following syntax:


if {
statement 1
   }
Else
{
Statement 2
}




Take a moment to examine this syntax. It begins with the reserved word if, followed by a logical expression contained within parentheses, followed by a statement, followed by the reserved word else, followed by a second statement. Statements 1 and 2 are any valid++ statements. Into-way selection, if the value of the expression is true, statement1 executes. If the value of the expression is false, statement2 executes.

Example:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
double wages, rate, hours;
cout << fixed << showpoint << setprecision(2);                     //Line 1
cout << "Line 2: Enter working hours and rate: ";                  //Line 2
cin >> hours >> rate;                                                                  //Line 3
if (hours > 40.0)                                                                          //Line 4
wages = 40.0 * rate +
1.5 * rate * (hours - 40.0);                                                           //Line 5
else                                                                                              //Line 6
wages = hours * rate;                                                                   //Line 7
cout << endl;                                                                                 //Line 8
cout << "Line 9: The wages are $" << wages
<< endl;                                                                                          //Line 9


Multiple Selections: Nested if

In the previous sections, you learned how to implement one-way and two-way selections in a program. Some problems require the implementation of more than two alternatives.
For example, suppose that if the checking account balance is more than $50,000, the interest rate is 7%; if the balance is between $25,000 and $49,999.99, the interest rate is
5%; if the balance is between $1,000 and $24,999.99, the interest rate is 3%; otherwise,
4Selection: if and if...else the interest rate is 0%. This particular problem has four alternatives—that is, multiple selection paths. You can include multiple selection paths in a program by using an if.. .else structure if the action statement itself is an if or if. . .else statement. When one control statement is located within another, it is said to be nested.

Example:
if (balance > 50000.00)                                                                                  //Line 1
interestRate = 0.07;                                                                                      //Line 2
else                                                                                                                //Line 3
if (balance >= 25000.00)                                                                               //Line 4
interestRate = 0.05;                                                                                     //Line 5
else                                                                                                                  //Line 6
if (balance >= 1000.00)                                                                                  //Line 7
interestRate = 0.03;                                                                                       //Line 8
else                                                                                                                   //Line 9
interestRate = 0.00;                                                                                       //Line 10
A nested if. . .else structure demands the answer to an important question: How do you know which else is paired with which if? Recall that in C++, there is no stand-alone else statement. Every else must be paired with an if. The rule to pair an else with an if is as follows:
Pairing an else with an if: In a nested if statement, C++ associates an else with the most recent incomplete if—that is, the most recent if that has not been paired with an else.

Comparing if...else Statements with a Series of if Statements

Consider the following C++ program segments, all of which accomplish the same task.

a
 if (month == 1)                                                                          //Line 1
cout << "January" << endl;                                                      //Line 2
else if (month == 2)                                                                   //Line 3
cout << "February" << endl;                                                     //Line 4
else if (month == 3)                                                                    //Line 5
cout << "March" << endl;                                                          //Line 6
else if (month == 4)                                                                     //Line 7
cout << "April" << endl;                                                                //Line 8
else if (month == 5)                                                                       //Line 9
cout << "May" << endl;                                                                //Line 10
else if (month == 6)                                                                     //Line 11
cout << "June" << endl;                                                              //Line 12

b  
 if (month == 1)
cout << "January" << endl;
if (month == 2)
cout << "February" << endl;
if (month == 3)
cout << "March" << endl;
if (month == 4)
cout << "April" << endl;
if (month == 5)
cout << "May" << endl;
if (month == 6)
cout << "June" << endl;

Program segment (a) is written as a sequence of if. . .else statements; program segment

(b) is written as a series of if statements. Both program segments accomplish the something. If month is 3, then both program segments output March. If month is 1, then in program segment (a), the expression in the if statement in Line 1 evaluates to true. The statement (in Line 2) associated with this if then executes; the rest of the structure, which is the else of this if statement, is skipped; and the remaining if statements are not evaluated. In program segment (b), the computer has to evaluate the expression in each if statement because there is no else statement. As a consequence, program segment (b) executes more slowly than does program segment (a).

0 comments:

Post a Comment