1 / 10

Switch Statements

Switch Statements. Switch Statement. Often you want to do a series of tests if i==0 … else if i==1 …. else if i==2 … else if i==3 …. C++ provides the switch statement to help in this situation

rowdy
Download Presentation

Switch Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Switch Statements

  2. Switch Statement • Often you want to do a series of tests • if i==0 … else if i==1 …. else if i==2 … else if i==3 …. • C++ provides the switch statement to help in this situation • It allows you to specify a large set of cases you want to be able to match, yet works efficiently to find and execute the particular case matched

  3. Format Example: ………… int mortgageTerm; float interestRate; cin>> mortgageTerm; Switch (mortgageTerm) { case 10: interestRate= 3.0; break; case 15: interestRate=3.5; break; case 30: interestRate=4.0; break; default: cout<<“please enter a valid loan term”; } • Format: • switch(switchExpression ){ • case value1: • statements; • break; • case value2: • statements; • break; • … • … • default: • statements; • }

  4. Examples char grade; cin >> grade; switch (grade) {         case 'A':  cout << "Great job!!";                        break;         case 'B':  cout << "Good job";                        break;         case 'C':  cout << "Satisfactory job";                        break;         case 'D':  cout << "Hmmm ... need to work a little harder";                        break;         case 'F':  cout << "Sorry, you failed the class";                        break;         default:  cout << "The letter you typed " << grade << " is not a valid grade"; }

  5. Examples: #include <iostream> using namespace std; int main() { int operand1 = 0, operand2 = 0, result=0; char operator = ‘ ‘; cout << “Please enter expression (num oper num) ? “; cin >> operand1 >> operator >> operand2; switch (operator) { case ‘+’: result = operand1 + operand2; break; case ‘-’: result = operand1 - operand2; break; // other cases left off for room default: cout << “Did not recognize operator” << endl; } cout << operand1 << “ “ << operator << “ “ <<operand2 << “ = “ << result << endl; return 0; }

  6. Examples int x, y; cin>>x>>y; switch (x>y) { case false: cout<<“x is no greater than y”; break; case true: cout<<“ x is greater than y”; break; default: } int x, y; cin>>x>>y; switch (x>y) { case 0: cout<<“x is no greater than y”; break; case 1: cout<<“ x is greater than y”; break; default: }

  7. Fall Through Example: Switch (flight_class) { Case 3: ticket=300; break; Case 2: ticket=500; break; Case 1:ticket=1000; break; Default: cout<<“unknown class!”; } …. switch (flight_class) { case 3: ticket=300; case 2: ticket=500; case 1: ticket=1000; } cout<<“you need to pay”<<“\” ticket <<“dollars, thank you!”<<endl; What the third class passenger will need to pay?

  8. Exercise Convert the following segment of code to switch statement: int j, n; ….. If (j==3 || j==5) n = 6; else if ( j==4 || j==8) n = 9; else if (j==2) n = 8; else n=0;

  9. Exercise: What the output will be? #include <iostream> using namespace std; int x=1, y=2, z=3; int main(){ switch (x>0) { case 1: switch (y<0) { case 1: cout<<“?”; break; case 2: cout<<“%”; break; } case 0: switch (z==3) { case 0: cout<<“+”; break; case 1: cout<<“#”; break; } default: cout<<“&”; } return 0; }

  10. Switch Statement • Style ideas for the switch statement • Unless you have many conditions (4 or more), use if-else-if instead of switch • Always provide a default case – if you are pretty sure you have all cases covered, putting an error message in the default is good to identify unexpected errors • Order the cases in some logical order (numeric, alphabetic) • Keep the size of each of the cases small • If you have to do lots of work in each case, call a function from inside the case

More Related