1 / 50

Switch-Case Statement

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.

malha
Download Presentation

Switch-Case Statement

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-Case Statement

  2. 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.

  3. switch-case Syntax switch (control_var){    case constant1:        statement1        break;    case constant2:        statement2        break;    .    .    .    case constantN:statementN        break;    default:default_statement}

  4. switch-case Syntax switch (control_var){    case constant1:        statement1        break;    case constant2:        statement2        break;    .    .    .    case constantN:statementN        break;    default:default_statement}

  5. switch-case Syntax switch (control_var){    case constant1:        statement1        break;    case constant2:        statement2        break;    .    .    .    case constantN:statementN        break;    default:default_statement}

  6. switch-case Syntax switch (control_var){    case constant1:        statement1        break;    case constant2:        statement2        break;    .    .    .    case constantN:statementN        break;    default:default_statement}

  7. switch-case Syntax switch (control_var){    case constant1:        statement1        break;    case constant2:        statement2        break;    .    .    .    case constantN:statementN        break;    default:default_statement}

  8. switch-case Syntax switch (control_var){    case constant1:        statement1        break;    case constant2:        statement2        break;    .    .    .    case constantN:statementN        break;    default:default_statement}

  9. switch-case Syntax switch (control_var){    case constant1:        statement1        break;    case constant2:        statement2        break;    .    .    .    case constantN:statementN        break;    default:default_statement}

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

  11. 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

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

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

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

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

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

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

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

  19. 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 );

  20. 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 );

  21. 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 );

  22. 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

  23. 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 );

  24. 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 );

  25. 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 );

  26. 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 );

  27. 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 );

  28. 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 );

  29. 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 );

  30. 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 );

  31. 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 );

  32. 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 );

  33. 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 );

  34. 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 );

  35. 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

  36. 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 );

  37. 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 );

  38. 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 );

  39. 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 );

  40. 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 );

  41. 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 );

  42. 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 );

  43. 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 );

  44. 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 );

  45. 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 );

  46. 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 );

  47. 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 );

  48. 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 );

  49. 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}

  50. End of Session

More Related