500 likes | 684 Views
Switch-Case Statement. What is a switch-case?. The switch-case statement is really nothing more than a glorified nested if-else package. C++ has taken on the headache of setting up the overhead of creating the structure of nested if-else for you. switch-case Syntax.
E N D
What is a switch-case? The switch-case statement is really nothing more than a glorified nested if-else package. C++ has taken on the headache of setting up the overhead of creating the structure of nested if-else for you.
switch-case Syntax switch (control_var){ case constant1: statement1 break; case constant2: statement2 break; . . . case constantN:statementN break; default:default_statement}
switch-case Syntax switch (control_var){ case constant1: statement1 break; case constant2: statement2 break; . . . case constantN:statementN break; default:default_statement}
switch-case Syntax switch (control_var){ case constant1: statement1 break; case constant2: statement2 break; . . . case constantN:statementN break; default:default_statement}
switch-case Syntax switch (control_var){ case constant1: statement1 break; case constant2: statement2 break; . . . case constantN:statementN break; default:default_statement}
switch-case Syntax switch (control_var){ case constant1: statement1 break; case constant2: statement2 break; . . . case constantN:statementN break; default:default_statement}
switch-case Syntax switch (control_var){ case constant1: statement1 break; case constant2: statement2 break; . . . case constantN:statementN break; default:default_statement}
switch-case Syntax switch (control_var){ case constant1: statement1 break; case constant2: statement2 break; . . . case constantN:statementN break; default:default_statement}
short choice;cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << endl << “\t\tYour choice: ”;cin >> choice;switch (choice){ case 1:cout << “Oink”; break; case 2: cout << “Bark”;cout << “Ruffruff”; break; case 3:cout << “Moooo” << endl; break;}
short choice;cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << endl << “\t\tYour choice: ”;cin >> choice;switch (choice){ case 1:cout << “Oink”; break; case 2: cout << “Bark”;cout << “Ruffruff”; break; case 3:cout << “Moooo” << endl; break;} user inputs: 2
short choice;cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << endl << “\t\tYour choice: ”;cin >> choice;switch (choice){ case 1:cout << “Oink”; break; case 2: cout << “Bark”;cout << “Ruffruff”; break; case 3:cout << “Moooo” << endl; break;}
short choice;cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << endl << “\t\tYour choice: ”;cin >> choice;switch (choice){ case 1:cout << “Oink”; break; case 2: cout << “Bark”;cout << “Ruffruff”; break; case 3:cout << “Moooo” << endl; break;}
short choice;cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << endl << “\t\tYour choice: ”;cin >> choice;switch (choice){ case 1:cout << “Oink”; break; case 2: cout << “Bark”;cout << “Ruffruff”; break; case 3:cout << “Moooo” << endl; break;}
short choice;cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << endl << “\t\tYour choice: ”;cin >> choice;switch (choice){ case 1:cout << “Oink”; break; case 2: cout << “Bark”;cout << “Ruffruff”; break; case 3:cout << “Moooo” << endl; break;}
short choice;cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << endl << “\t\tYour choice: ”;cin >> choice;switch (choice){ case 1:cout << “Oink”; break; case 2: cout << “Bark”;cout << “Ruffruff”; break; case 3:cout << “Moooo” << endl; break;}
short choice;cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << endl << “\t\tYour choice: ”;cin >> choice;switch (choice){ case 1:cout << “Oink”; break; case 2: cout << “Bark”;cout << “Ruffruff”; break; case 3:cout << “Moooo” << endl; break;}
short choice;cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << endl << “\t\tYour choice: ”;cin >> choice;switch (choice){ case 1:cout << “Oink”; break; case 2: cout << “Bark”;cout << “Ruffruff”; break; case 3:cout << “Moooo” << endl; break;}
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit );
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit );
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit );
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit ); user inputs: c
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit );
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit );
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit );
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit );
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit );
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit );
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit );
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit );
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit );
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit );
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit );
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit );
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit ); user inputs: 4
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit );
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit );
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit );
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit );
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit );
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit );
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit );
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit );
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit );
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit );
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit );
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit );
bool quit = false; char choice; do{cout << “\t\tAnimal Sounds” << endl << endl << “\t1. Pig” << endl << “\t2. Dog” << endl << “\t3. Cow” << endl << “\t4. Quit” << endl << endl << “\t\tYour choice: ”;cin >> choice; switch (choice) { case ‘1’: case ‘p’:cout << “Oink”; break; case ‘2’: case ‘d’:cout << “Bark”; break; case ‘3’: case ‘c’:cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default:cout << “ERROR: Invalid input...” << endl; }} while ( !quit );
long student_number;short student_number_index;cout << "enter student number: ";cin >> student_number;student_number_index = student_number / 10000; switch (student_number_index){ case 12: // code to handle transfers break; case 13: // code to handle freshmen break; case 14: // code to handle sophomores break; case 35: // code to handle grads default: // Error condition}