The if else if else Statement


The if /else if /else Statement

The program we used to illustrate the if/else statement involved only two alternatives. Additionally, these alternatives were mutually exclusive; only one could be chosen , not both. A whole number is either even or odd; it can t be both and there is no third alterative. There are many other examples of only two mutually exclusive alternatives. For example, a person is either dead or alive , male or female , child or adult.

However, there are other scenarios where there are more than two, mutually exclusive alternatives. For example, if you take a test, your grade may be one of five types: A, B, C, D, or F. Additionally, these grades are mutually exclusive; you can t get an A and a C on the same test.

Since you can have only one if expression and only one else expression in an if statement, you need another expression for the third and additional alternatives. That expression is else if.

You use the if / else if / else statement when there are three or more mutually exclusive alternatives. The if / else if / else statement has an if part and an else part, like an if/else statement. However, it also has one or more else if parts .

Note  

While the if part is required, the else part is not. Without it, the statement would be named an if / else if statement.

The else if part works similarly to an if expression. The else if keywords are followed by a relational expression. If the expression is true, then the conditional statement or statements belonging to the else if part execute. Otherwise, they don t.

While an if statement may include only one if part and one else part, it may include multiple else if parts.

The following program shows the if /else if /else statement in action in a program that determines your grade based on your test score.

 #include <iostream> using namespace std; int main(void) {  int testScore;  cout << "Enter your test score: ";  cin >> testScore;  if (testScore >= 90)  cout << "Your grade is an A" << endl;  else if (testScore >= 80)  cout << "Your grade is a B" << endl;  else if (testScore >= 70)  cout << "Your grade is a C" << endl;  else if (testScore >= 60)  cout << "Your grade is a D" << endl;  else   cout << "Your grade is an F" << endl;  return 0; } 

Here are several sample runs, each separated by a dotted line:

 Enter your test score: 77 Your grade is a C ------ Enter your test score: 91 Your grade is an A ------ Enter your test score: 55 Your grade is an F 

Figure 5-5 uses a flowchart to illustrate this program.

click to expand
Figure 5-5: Flowchart depiction of grading program

In this program, if your test score is 90 or better, then the conditional statement belonging to the if part executes, displaying that you received an A. The relational expressions of each of the following else if parts also are true; if your score is 90 or better, it also is 80 or better, 70 or better, and so on. However, in an if / else if / else statement, only the conditional statements in the first part whose relational expression is true will execute; the remaining parts are skipped .

Common Syntax Errors

The common syntax errors for the if part discussed earlier in this chapter apply to the else if part also. Don t put a semicolon after the relational expression, and multiple conditional statements must be enclosed in curly braces.

Additionally, just as you cannot have an else part without a preceding if part, you cannot have an else if part without a preceding if part. However, you may have an if part and one or more else if parts without an else part. The downside in omitting the else part is you will not have code to cover the none of the above scenario in which none of the relational expressions belonging to the if part and else if parts is true.




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