+ All Categories
Home > Documents > Switch-Case Statement

Switch-Case Statement

Date post: 19-Jan-2016
Category:
Upload: arnaud
View: 33 times
Download: 0 times
Share this document with a friend
Description:
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. - PowerPoint PPT Presentation
Popular Tags:
50
http://cs.mst.edu Switch-Case Statement
Transcript
Page 1: Switch-Case Statement

http://cs.mst.edu

Switch-Case Statement

Page 2: Switch-Case Statement

http://cs.mst.edu

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.

Page 3: Switch-Case Statement

http://cs.mst.edu

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

Page 4: Switch-Case Statement

http://cs.mst.edu

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

Page 5: Switch-Case Statement

http://cs.mst.edu

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

Page 6: Switch-Case Statement

http://cs.mst.edu

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

Page 7: Switch-Case Statement

http://cs.mst.edu

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

Page 8: Switch-Case Statement

http://cs.mst.edu

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

Page 9: Switch-Case Statement

http://cs.mst.edu

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

Page 10: Switch-Case Statement

http://cs.mst.edu

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

Page 11: Switch-Case Statement

http://cs.mst.edu

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

Page 12: Switch-Case Statement

http://cs.mst.edu

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

Page 13: Switch-Case Statement

http://cs.mst.edu

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

Page 14: Switch-Case Statement

http://cs.mst.edu

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

Page 15: Switch-Case Statement

http://cs.mst.edu

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

Page 16: Switch-Case Statement

http://cs.mst.edu

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

Page 17: Switch-Case Statement

http://cs.mst.edu

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

Page 18: Switch-Case Statement

http://cs.mst.edu

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

Page 19: Switch-Case Statement

http://cs.mst.edu

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

Page 20: Switch-Case Statement

http://cs.mst.edu

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

Page 21: Switch-Case Statement

http://cs.mst.edu

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

Page 22: Switch-Case Statement

http://cs.mst.edu

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

Page 23: Switch-Case Statement

http://cs.mst.edu

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

Page 24: Switch-Case Statement

http://cs.mst.edu

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

Page 25: Switch-Case Statement

http://cs.mst.edu

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

Page 26: Switch-Case Statement

http://cs.mst.edu

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

Page 27: Switch-Case Statement

http://cs.mst.edu

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

Page 28: Switch-Case Statement

http://cs.mst.edu

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

Page 29: Switch-Case Statement

http://cs.mst.edu

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

Page 30: Switch-Case Statement

http://cs.mst.edu

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

Page 31: Switch-Case Statement

http://cs.mst.edu

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

Page 32: Switch-Case Statement

http://cs.mst.edu

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

Page 33: Switch-Case Statement

http://cs.mst.edu

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

Page 34: Switch-Case Statement

http://cs.mst.edu

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

Page 35: Switch-Case Statement

http://cs.mst.edu

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

Page 36: Switch-Case Statement

http://cs.mst.edu

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

Page 37: Switch-Case Statement

http://cs.mst.edu

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

Page 38: Switch-Case Statement

http://cs.mst.edu

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

Page 39: Switch-Case Statement

http://cs.mst.edu

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

Page 40: Switch-Case Statement

http://cs.mst.edu

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

Page 41: Switch-Case Statement

http://cs.mst.edu

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

Page 42: Switch-Case Statement

http://cs.mst.edu

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

Page 43: Switch-Case Statement

http://cs.mst.edu

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

Page 44: Switch-Case Statement

http://cs.mst.edu

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

Page 45: Switch-Case Statement

http://cs.mst.edu

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

Page 46: Switch-Case Statement

http://cs.mst.edu

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

Page 47: Switch-Case Statement

http://cs.mst.edu

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

Page 48: Switch-Case Statement

http://cs.mst.edu

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

Page 49: Switch-Case Statement

http://cs.mst.edu

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}

Page 50: Switch-Case Statement

http://cs.mst.edu

End of Session


Recommended