#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 int UNKNOWN = -123; const float MAX_WITHDRAWAL = 500.0; double balance1 = 100.0; double balance2 = 200.0; /* * Switch / Case Equivalent to If / Else If */ cout << endl << "Switch / Case Equivalent to If / Else If" << endl; int transaction = DEPOSIT; double amount = 250.0; cout << "\nDeposit Example" << endl; cout << "balance1: " << balance1 << endl; cout << "depositing: " << amount << endl; switch ( transaction ) { case DEPOSIT : balance1 = balance1 + amount; break; case WITHDRAWAL : balance1 = balance1 - amount; break; case TRANSFER : balance1 = balance1 - amount; balance2 = balance2 - amount; break; } cout << "New balance1: " << balance1 << endl; getch(); // return 0; /* * Default Clause */ cout << endl << "Default Clause" << endl; transaction = UNKNOWN; amount = 250.0; cout << "\nDefault Example" << endl; cout << "balance1: " << balance1 << endl; switch ( transaction ) { case DEPOSIT : balance1 = balance1 + amount; break; case WITHDRAWAL : balance1 = balance1 - amount; break; case TRANSFER : balance1 = balance1 - amount; balance2 = balance2 - amount; break; default: cout << "Unknown Transaction" << endl; } cout << "New balance1: " << balance1 << endl; getch(); // return 0; /* * Switch on Characters and No Breaks */ cout << endl << "Switch on Characters and No Breaks" << endl; char character = 'a'; int capitalA = 0, letterA = 0, totalCharacters = 0; switch ( character ) { case 'A': capitalA = capitalA + 1; case 'a': letterA = letterA + 1; default : totalCharacters = totalCharacters + 1; } cout << "character: " << character << endl; cout << "capitalA: " << capitalA << endl; cout << "letterA: " << letterA << endl; cout << "totalCharacters: " << totalCharacters << endl; getch(); // return 0; /* * Combining Cases Together as OR */ cout << endl << "Combining Cases Together as OR" << endl; int choice = 6; cout << "choice: " << choice << endl; switch (choice) { case 0: case 1: case 2: cout << "Less than 3!"; break; case 3: cout << "Equals 3!"; break; default: cout << "Greater than 3!"; } getch(); return 0; }