switch statements. switch statement often you want to do a series of tests –if i==0 … else if...

10
Switch Statements

Upload: brianna-welch

Post on 23-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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 Statements

Page 2: 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• 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

Page 3: 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

Format

• Format: switch (switchExpression ) {

case value1:

statements;

break;

case value2:

statements;

break;

default:

statements;

}

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”;}

Page 4: 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

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";                      }

Examples

Page 5: 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

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;}

Page 6: 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

Examples

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:

}

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:

}

Page 7: 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

Fall Through Example:….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?

Switch (flight_class)

{

Case 3: ticket=300;

break;

Case 2: ticket=500;

break;

Case 1:ticket=1000;

break;

Default: cout<<“unknown class!”;

}

Page 8: 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

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;

Page 9: 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

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;}

Page 10: 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• 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