The if else Statement


The if / else Statement

One problem with the program that tests whether a number is even is that there is no output if the number is odd. While there is a conditional statement if the relational expression is true, there is no corresponding conditional statement (cout << The number is odd) if the relational expression is false.

The solution is to add an else part to the if statement. The result is an if / else statement. The syntax of an if / else statement is

 if (relational expression)  conditional statement; else  conditional statement; 

Accordingly, the program may be modified to add an else part to the if statement:

 #include <iostream> using namespace std; int main(void) {  int num;  cout << "Enter a whole number: ";  cin >> num;  if (num % 2 == 0)  cout << "The number is even" << endl;  else  cout << "The number is odd" << endl;  return 0; } 

Run this code. If the inputted number is even, then the output once again is The number is even. However, if the number is now odd, instead of no output, the output is The number is odd.

 Enter a whole number: 17 The number is odd 

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

click to expand
Figure 5-4: Flowchart of program output if number is even or odd

Conditional Operator

This program could be rewritten using the conditional operator.

 #include <iostream> using namespace std; int main(void) {  int num;  cout << "Enter a whole number: ";  cin >> num;  cout << "The number is " << (num % 2 == 0 ? "even" :   "odd") << endl;  return 0; } 

The syntax of the conditional operator is

 [Relational expression] ? [statement if true] :  [statement if false] 

In this example, the relational expression is num % 2 == 0 . If the value of the relational expression is true, then the output is even. However, if the value of the relational expression is false, then the output is odd.

The conditional operator requires three operands, the relational expression and the two conditional statements. Therefore, it is considered a ternary operator.

Common Mistakes

Just as with the if statement, I noticed several common syntax mistakes with the else statement while teaching C++ in introductory programming classes.

No else Without an if

You can have an if expression without an else part. However, you cannot have an else part without an if part. The else part must be part of an overall if statement. This requirement is logical. The else part works as none of the above ; without an if part there is no above.

As a consequence, placing a semicolon after the Boolean expression following the if keyword will result in a compiler error. Since curly braces are not used, the if statement ends after the empty statement created by the incorrectly placed semicolon. The cout statement The number is even is not part of the if statement. Consequently, the else part is not part of the if statement, and therefore will be regarded as an else part without an if part.

 if (num % 2 == 0); // don't put a semicolon here  cout << "The number is even" << endl;  else (num % 2 == 1)  cout << "The number is odd" << endl; 

Don t Put a Relational Expression after  the  else  Keyword!

Another common mistake is to place a relational expression in parentheses after the else keyword. This will not cause a compiler or run-time error, but it will often cause an illogical result.

 if (num % 2 == 0)  cout << "The number is even" << endl;  else (num % 2 == 1)  cout << "The number is odd" << endl; 

The program will not compile, and the cout statement following the else expression will be highlighted with an error description such as missing ˜; before identifier ˜cout .

Actually, the error description is misleading. There is nothing wrong with the cout statement. Instead, no relational expression should follow the else keyword. The reason is that the else acts like none of the above in a multiple choice test. If the if expression is not true, then the conditional statements connected to the else part execute.

Don t Put a Semicolon after the Else!

Another common mistake is to place a semicolon after the else expression. This too will not cause a compiler or run-time error, but often will cause an illogical result.

For example, in the following code, the cout statement The number is odd will output even if the number that s input is even.

 if (num % 2 == 0)  cout << "The number is even" << endl;  else; // don't put a semicolon here!  cout << "The number is odd" << endl; 

The result of inputting an even number will be

 Enter a whole number: 16 The number is even The number is odd 

The cout statement The number is odd will execute whether or not the relational expression is true because the cout statement no longer is part of the if statement. Unless you use curly braces as explained already in connection with the if statement, only the first statement following the else keyword is conditional. That first, conditional statement is the empty statement by virtue of the semicolon following the if expression. Therefore, the cout statement The number is odd is not part of the if statement at all.

Curly Braces Are Needed for Multiple  Conditional  Statements

As with the if expression, if you want more than one conditional statement to belong to the else part, then you must encase the statements in curly braces. For example, in the following code fragment, the cout statement This also belongs to the else part will always display whether the number is even or odd since it does not belong to the if statement.

 if (num % 2 == 0)  cout << "The number is even" << endl; else  cout << "The number is odd" << endl; cout << "This also belongs to the else part"; 

The sample input and output could be

 Enter a whole number: 16 The number is even This also belongs to the else part 

Encasing the multiple conditional statements in curly braces solves this issue.

 if (num % 2 == 0)  cout << "The number is even" << endl; else {  cout << "The number is odd" << endl;  cout << "This also belongs to the else part"; } 



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