Changing the Course of Execution

In our examples so far, the program always executes the same statements in the same order. However, any real program is going to need to alter its behavior according to the data it is processing. For example, in a banking application, it might be necessary to send out a notice to a depositor whenever the balance in a particular account drops below a certain level; or perhaps the depositor would just be charged some exorbitant fee in that case. Either way, the program has to do something different depending on the balance. In particular, let's suppose that the “Absconders and Defaulters National Bank” has a minimum balance requirement of $10,000.[37] Furthermore, let's assume that if you have less than that amount on deposit, you are charged a $20 “service charge”. However, if you are foolish enough to leave that ridiculous amount of money on deposit, then they will graciously allow you to get away with not paying them while they're using your money (without paying you interest, of course). To determine whether you should be charged for your checking account, the bank can use an if statement, as shown in Figure 3.22.

[37] If you think the name of that bank is funny, you should get a copy of The Most of S. J. Perelman (ISBN 0-671-41871-8). Unfortunately, it's out of print, but you might be able to find a used one at Amazon.com or another bookseller.

Figure 3.22. Using an if statement (codeasic03.cpp)
#include <iostream>
using namespace std;

int main()
{
    short balance;

    cout << "Please enter your bank balance:";
    cin >> balance;

    if (balance < 10000)
        cout << "Please remit $20 service charge." << endl;
    else
        cout << "Have a nice day!" << endl;

    return 0;
}

This program starts by displaying the line

Please enter your bank balance:

on the screen. Then it waits for you to type in your balance, followed by the ENTER key (so it knows when you're done). The conditional statement checks whether you're a “good customer”. If your balance is less than $10,000, the next statement is executed, which displays

Please remit $20 service charge.[38]
				

[38] This explanation assumes that the "10000" is the balance in dollars. Of course, this doesn't account for the possibility of balances that aren't a whole number of dollars, and there's also the problem of balances greater than $32767, which wouldn't fit into a short. As we'll see in Chapter 9, both of these problems can be solved by using a different data type called double.

The phrase << endl is new here. It means “we're done with this line of output; send it out to the screen”. You could also use the special character ' ' (“newline”), which means much the same thing.

Now let's get back to our regularly scheduled program. If the condition is false (that is, you have at least $10,000 in the bank), the computer skips the statement that asks you to remit $20; instead, it executes the one after the else, which tells you to have a nice day. That's what else is for; it specifies what to do if the condition specified in the if statement is false (that is, not true). If you typed in a number 10000 or higher, the program would display the line

Have a nice day!

You don't have to specify an else if you don't want to. In that case, if the if condition isn't true, the program just goes to the next statement as though the if had never been executed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset