switch case statement
Download
Skip this Video
Download Presentation
Switch-Case Statement

Loading in 2 Seconds...

play fullscreen
1 / 50

Switch-Case Statement - PowerPoint PPT Presentation


  • 123 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Switch-Case Statement' - malha


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
what is a switch case
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-case Syntax

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

switch case syntax1
switch-case Syntax

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

switch case syntax2
switch-case Syntax

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

switch case syntax3
switch-case Syntax

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

switch case syntax4
switch-case Syntax

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

switch case syntax5
switch-case Syntax

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

switch case syntax6
switch-case Syntax

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

slide10

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

slide11

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

slide12

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

slide13

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

slide14

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

slide15

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

slide16

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

slide17

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

slide18

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

slide19

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

slide20

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

slide21

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

slide22

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

slide23

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

slide24

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

slide25

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

slide26

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

slide27

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

slide28

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

slide29

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

slide30

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

slide31

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

slide32

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

slide33

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

slide34

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

slide35

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

slide36

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

slide37

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

slide38

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

slide39

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

slide40

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

slide41

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

slide42

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

slide43

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

slide44

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

slide45

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

slide46

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

slide47

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

slide48

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

slide49

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}

ad