Nested if Statements


An if statement may appear inside another if statement. When this is done, the inner if statement is said to be nested inside the outer if statement.

You can nest if statements to determine if both of two Boolean expressions are true, or if either of the expressions is true.

Testing if Both Boolean Expressions Are True

The following program shows the use of nested if statements in determining if both of two Boolean expressions are true. If the user s input is that they are at least 18 years old and a citizen, the program outputs that they are eligible to vote. Otherwise, the program outputs that they are not eligible to vote.

 #include <iostream> using namespace std; int main(void) {  int age;  char choice;  bool citizen;  cout << "Enter your age: ";  cin >> age;  cout << "Are you a citizen (Y/N): ";  cin >> choice;  if (choice == 'Y')  citizen = true;  else  citizen = false;  if (age >= 18)  if(citizen == true)  cout << "You are eligible to vote";  else  cout << "You are not eligible to vote";  else  cout << "You are not eligible to vote";  return 0; } 

The following are several sample runs, each separated by ===:

 Enter your age: 18 Are you a citizen (Y/N): Y You are eligible to vote === Enter your age: 18 Are you a citizen (Y/N): N You are not eligible to vote === Enter your age: 17 Are you a citizen (Y/N): Y You are not eligible to vote ===  Enter your age: 17 Are you a citizen (Y/N): N You are not eligible to vote === 

Figure 6-1 depicts a flowchart of this program.

click to expand
Figure 6-1: Flowchart of the voting eligibility program

The nested if portion of the program is

 if (age >= 18)  if(citizen == true)  cout << "You are eligible to vote";  else  cout << "You are not eligible to vote";  else  cout << "You are not eligible to vote"; 
Note  

The statement if(citizen == true) could be rewritten as if(citizen). The parentheses following the if keyword requires only an expression that evaluates to a Boolean value. Since citizen is a Boolean variable, it evaluates to a Boolean value without the need for any comparison.

The if/else structure comparing whether the user is a citizen is nested within the if/else structure comparing whether the user is at least 18 years old. By this nesting, the comparison of whether the user is a citizen is made only if the user is at least 18 years old. This approach is logical, since if the user is not at least 18 years old, they will not be eligible to vote even if they are a citizen.

The if / else structure comparing whether the user is a citizen is referred to as the inner if / else structure. The if / else structure comparing whether the user is at least 18 years old is referred to as the outer if / else structure.

The entire inner if / else structure (comparing whether the user is a citizen) is nested within the if part of the outer if / else structure (comparing whether the user is at least 18 years old). You also can nest an if / else structure (or an if structure, or an if /else if /else structure) within the else if or else part of an outer if else/if else if else/if else structure.

This program illustrates a good use of nested if statements. It would be difficult to rewrite this program using an if / else if / else structure without nested if statements. However, later in this chapter we will cover another, equally good alternative: logical operators.

Testing if Either Boolean Expression Is True

The following program shows the use of the nested if statements in determining if either of two Boolean expressions are true. If the user s input indicates that they are either no more than 12 years old or at least 65 years old, the program outputs that their admission is free. Otherwise, the program outputs that they have to pay.

 #include <iostream> using namespace std; int main(void) {  int age;  cout << "Enter your age: ";  cin >> age;  if (age > 12)  if (age >= 65)  cout << "Admission is free";  else  cout << "You have to pay";  else  cout << "Admission is free";  return 0; } 

The following shows several sample runs:

 Enter your age: 12 Admission is free === Enter your age: 13 You have to pay === Enter your age: 65 Admission is free 

Figure 6-2 depicts a flowchart of this program.

click to expand
Figure 6-2: Flowchart of the movie admission program

The nested if portion of the program is

 if (age > 12)  if (age >= 65)  cout << "Admission is free";  else  cout << "You have to pay";  else  cout << "Admission is free"; 

The inner if/else structure, comparing whether the user is at least 65 years old, is nested within the outer if/else structure, comparing whether the user is over 12 years old. By this nesting, the comparison of whether the user is at least 65 years old is made only if the user is over 12 years old. This approach is logical, since if the user is no more than 12 years old, they will be admitted free (and also could not possibly be 65 years or older).

This program also could have been written using the following if / else if / else structure in place of the nested if statements:

 if (age <= 12)  cout << "Admission is free";  else if (age >= 65)  cout << "Admission is free";  else  cout << "You have to pay"; 

Each of these two alternatives, the nested if statements and the if / else if / else structure, have disadvantages. Nesting one if statement inside another by its very nature may be somewhat difficult to write and understand. However, the if / elseif/else if / else structure has the disadvantage of repeating the same cout statement for both the if and else if parts . While this is just one line of repetitive code in this program, in more complex programs the repetitive code could be many lines long.

C++ has a third and perhaps better alternative, the use of logical operators, which we will discuss next .




C++ Demystified(c) A Self-Teaching Guide
C++ Demystified(c) A Self-Teaching Guide
ISBN: 72253703
EAN: N/A
Year: 2006
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net