#include #include using namespace std; int main() { /* * Bank Example */ cout << endl << "Bank Example" << endl; const int WITHDRAWAL = 0; const int DEPOSIT = 1; const int TRANSFER = 2; const float MAX_WITHDRAWAL = 500.0; double balance1 = 100.0; double balance2 = 200.0; getch(); /* * If Single Statement */ cout << endl << "If Single Statement" << endl; int transaction = DEPOSIT; double amount = 250.0; cout << "\nDeposit Example" << endl; cout << "balance1: " << balance1 << endl; cout << "depositing: " << amount << endl; if ( transaction == DEPOSIT ) balance1 = balance1 + amount; if ( transaction == WITHDRAWAL ) balance1 = balance1 - amount; cout << "balance1: " << balance1 << endl; getch(); /* * If Code Block */ cout << endl << "If Code Block" << endl; transaction = TRANSFER; amount = 50.0; cout << "\nTransfer Example" << endl; cout << "\nBefore Transfer" << endl; cout << "balance1: " << balance1 << endl; cout << "balance2: " << balance2 << endl; cout << "\ntransfering: " << amount << "\tfrom balance1 to balance2" << endl; if ( transaction == DEPOSIT ) balance1 = balance1 + amount; if ( transaction == WITHDRAWAL ) balance1 = balance1 - amount; if ( transaction == TRANSFER ) { // assume we are transferring from balance1 to 2 balance1 = balance1 - amount; balance2 = balance2 + amount; } cout << "\nAfter Transfer" << endl; cout << "balance1: " << balance1 << endl; cout << "balance2: " << balance2 << endl; getch(); /* * If / Else */ cout << endl << "If / Else" << endl; const int SOME_UNSUPPORTED_TRANSACTION = -123; transaction = SOME_UNSUPPORTED_TRANSACTION; if ( transaction == DEPOSIT ) balance1 = balance1 + amount; else cout << "Unkown Transaction" << endl; getch(); /* * If / Else If */ cout << endl << "If / Else If" << endl; if ( transaction == DEPOSIT ) balance1 = balance1 + amount; else if ( transaction == WITHDRAWAL ) balance1 = balance1 - amount; else if ( transaction == TRANSFER ) { balance1 = balance1 - amount; balance2 = balance2 + amount; } else cout << "Unkown Transaction" << endl; getch(); /* * Nested If / Else If */ cout << endl << "Nested If / Else If" << endl; cout << "Before transaction" << endl; cout << "balance1: " << balance1 << endl; cout << "balance2: " << balance2 << endl; cout << "transaction amount: " << amount << endl; transaction = TRANSFER; if ( transaction == DEPOSIT ) balance1 = balance1 + amount; else if ( transaction == WITHDRAWAL ) balance1 = balance1 - amount; else if ( transaction == TRANSFER ) { const int ACCOUNT1 = 1; const int ACCOUNT2 = 2; int from = ACCOUNT1; int to = ACCOUNT2; if ( from == ACCOUNT1 && to == ACCOUNT2 ) { balance1 = balance1 - amount; balance2 = balance2 + amount; } else if ( from == ACCOUNT2 && to == ACCOUNT1 ) { balance2 = balance2 - amount; balance1 = balance1 + amount; } } else cout << "Unkown Transaction" << endl; cout << "\nAfter transaction" << endl; cout << "balance1: " << balance1 << endl; cout << "balance2: " << balance2 << endl; getch(); return 0; }