The If-Else Statement


The if-else statement gives C++ programs a way to make decisions based on the information the program has as it's running. Let's use listing 3.7 to see how it works.

Listing 3.7. A simple if-else statement

 1    #include <cstdlib> 2    #include <iostream> 3 4    using namespace std; 5 6    int main(int argc, char *argv[]) 7    { 8        cout << "Please input a number greater than or equal to 0: "; 9 10       int aNumber; 11 12       cin >> aNumber; 13 14       if (aNumber >= 0) 15       { 16           cout << "Very good." << endl; 17       } 18       else 19       { 20       cout << "Not very good." << endl; 21       } 22 23       system("PAUSE"); 24       return (EXIT_SUCCESS); 25   } 

You create an if-else statement to make a decision, as Listing 3.7 illustrates. In this example, the program asks the user to enter a number. It makes the decision on lines 1421. If the user entered a number greater than or equal to 0, the program prints a compliment (line 16). If the number the user entered is less than 0, the program prints another message (line 20).

All if-else statements follow the format of the one in Listing 3.7 To be specific, they all start with the C++ keyword if, which is always followed by a logical expression in parentheses. Like a loop, if-else statements can have bodies. Of course, the bodies are enclosed in opening and closing braces. The if-else statement is actually composed of two parts, or clauses. They all have a required if clause and an optional else clause. The if clause for the program in Listing 3.7 appears on lines 1417. As you can see, the if clause has a code body on lines 1517. The body of the else clause spans lines 1921.

Because the if and else clauses in an if-else statement each have their own code bodies, the program executes different blocks of code based on the condition. In other words, if the condition on line 14 is true, the program executes the body of the if clause on lines 1517. On the other hand, when the condition on line 14 is false, the program executes the code body of the else clause. As a result, the program prints the string on line 16 if the condition is TRue. If the condition is false, it prints the string on line 20.

With all if-else statements, the else clause is optional. You don't have to put it in. Listing 3.8 shows the same if-else statement without an else clause.

Listing 3.8. An if statement without an else clause

 1    #include <cstdlib> 2    #include <iostream> 3 4    using namespace std; 5 6    int main(int argc, char *argv[]) 7    { 8       cout << "Please input a number greater than or equal to 0: "; 9 10      int aNumber; 11 12      cin >> aNumber; 13 14      if (aNumber >= 0) 15      { 16      cout << "Very good." << endl; 17      } 18 19      system("PAUSE"); 20      return (EXIT_SUCCESS); 21   } 

If you run this program and the condition is TRue, it executes the body of the if statement. However, when you run this program and the condition evaluates to false, the program skips to the next statement after the if, which is on line 19. Essentially, this if does nothing when its condition is false.

Factoid

Programmers often refer to an if without an else as an if statement. When an else clause is present, they tend to call it an if-else statement.




Creating Games in C++(c) A Step-by-Step Guide
Creating Games in C++: A Step-by-Step Guide
ISBN: 0735714347
EAN: 2147483647
Year: N/A
Pages: 148

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