// Lab 5 Lesson #include #include #include using namespace std; int main() { // ------------------------ Review If/Else --------------------------------- // Switch statements function similarly to an if...else statement. // The following can be rewritten as a switch statement. char letter_grade; float GPA; letter_grade = 'B'; // IF-ELSE statement to determine letter grade if(letter_grade == 'A'){ GPA = 4.0; } else if (letter_grade == 'B'){ GPA = 3.0; } else if (letter_grade == 'C'){ GPA = 2.0; } else if (letter_grade == 'D'){ GPA = 1.0; } else if (letter_grade == 'F'){ GPA = 0.0; } else{ cout << "invalid input" << endl; } cout << "Using IF Statements : " << setprecision(1) << fixed << GPA << endl; // ------------------------ Switch Statement ---------------------------------- // Switch Statement can do the same thing // Switch syntax is as follows: // switch (expression){ // case constant-expression: // // statements // break; // } // The switch's expression is compared to each case's expression. // if the expressions are equal to eachother it will execute any statements // that follow the case, until hitting a break statement. Break statements // tell the program to leave the switch statement and continue after it. // This switch statement does the same thing as the above IF-ELSE IF code. switch (letter_grade){ case 'A': GPA = 4.0; break; // break statements signal to leave the switch statement case 'B': GPA = 3.0; break; case 'C': GPA = 2.0; break; case 'D': GPA = 1.0; break; default: // default is like an else statement cout << "invalid input" << endl; break; } cout << "Using Switch Statement : " << setprecision(1) << fixed << GPA << endl; // ------------------ Multiple Cases in a Single Break ------------------------ // What if you wanted to allow the user input to not be case sensitive? // We can have multiple cases assigned to execute the same code. In the // following snippet, both an upper case and lower case letter will execute // the same thing. letter_grade = 'b'; switch (letter_grade){ case 'A': // same as if(letter_grade == 'A' || letter_grade == 'a') case 'a': GPA = 4.0; break; case 'B': case 'b': GPA = 3.0; break; case 'C': case 'c': GPA = 2.0; break; case 'D': case 'd': GPA = 1.0; break; default: //default is like an else statement cout << "invalid input" << endl; break; } cout << "Using Lower Case Input : " << setprecision(1) << fixed << GPA << endl; // ----------------- Disadvantages of a switch statement ---------------------_ // Consider the following if...else statement int num = 85; if(num <= 89 && num >= 80){ cout << "Your letter grade is a B (If Statement)" << endl; }else{ cout << "out of range" << endl; } // you cannot easily implement a range with a switch statement. // the only possible way to duplicate the above if...else Statement is // to list each case out individually. Both if...else statements and switch // statements haves pros and cons which should be weighed before // implementing one or the other. switch(num){ case 89: case 88: case 87: case 86: case 85: case 84: case 83: case 82: case 81: case 80: cout << "your letter grade is a B (Switch Statment)" << endl; break; default: cout << "out of range" << endl; break; } // ------------------------ Switch Example ---------------------------------- // Another example of a case statement // Can you understand it? As you can see, as programs // get more complex using good style is crucial for // readability. // This is another example where using if...else statements // might be better as it would likely be more readable. const double PI = 3.14159; int choice_A; int choice_B; double total; cout << "\n\nWelcome to my shape calculator!" << endl << endl << "Please select one of the following" << endl << "1. Circle" << endl << "2. Square " << endl; cin >> choice_A; switch(choice_A){ case 1: double radius; cout << "You have selected Circle!" << endl << endl << "Please enter the radius of your circle" << endl; cin >> radius; cout << "Please select one of the following" << endl << "1. Area" << endl << "2. Circumference" << endl; cin >> choice_B; switch(choice_B){ case 1: total = PI * pow(radius, 2.0); cout << "The Area is " << total << endl; break; case 2: total = PI * 2 * radius; cout << "The Circumference is " << total << endl; break; default: cout << "invalid input. Terminating the program." << endl; break; } break; case 2: double length; cout << "You have selected Square!" << endl << endl << "Please enter the length of a side." << endl; cin >> length; cout << "Please select one of the following" << endl << "1. Area" << endl << "2. Perimeter" << endl; cin >> choice_B; switch(choice_B){ case 1: total = pow(length, 2.0); cout << "The Area is " << total; break; case 2: total = length * 4; cout << "The Perimeter is " << total; break; default: cout << "invalid input. Terminating the program." << endl; break; } break; default: cout << "Invalid input. Terminating the program." << endl; break; } // ---------------------------------------------------------------------------- return 0; }