Switch-Case Statement

Post on 19-Jan-2016

33 views 0 download

Tags:

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

transcript

http://cs.mst.edu

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.

http://cs.mst.edu

switch-case Syntaxswitch (control_var){    case constant1:        statement1        break;    case constant2:        statement2        break;    .    .    .    case constantN:        statementN        break;    default:        default_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}

http://cs.mst.edu

switch-case Syntaxswitch (control_var){    case constant1:        statement1        break;    case constant2:        statement2        break;    .    .    .    case constantN:        statementN        break;    default:        default_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}

http://cs.mst.edu

switch-case Syntaxswitch (control_var){    case constant1:        statement1        break;    case constant2:        statement2        break;    .    .    .    case constantN:        statementN        break;    default:        default_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}

http://cs.mst.edu

switch-case Syntaxswitch (control_var){    case constant1:        statement1        break;    case constant2:        statement2        break;    .    .    .    case constantN:        statementN        break;    default:        default_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;}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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}

http://cs.mst.edu

End of Session