Presentation is loading. Please wait.

Presentation is loading. Please wait.

Switch-Case Statement. What is a switch-case?  The switch-case statement is really nothing more than a glorified.

Similar presentations


Presentation on theme: "Switch-Case Statement. What is a switch-case?  The switch-case statement is really nothing more than a glorified."— Presentation transcript:

1 http://cs.mst.edu Switch-Case Statement

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

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

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

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

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

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

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

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

10 http://cs.mst.edu short choice; cout > choice; switch (choice) { case 1: cout << “Oink”; break; case 2: cout << “Bark”; cout << “Ruffruff”; break; case 3: cout << “Moooo” << endl; break; }

11 http://cs.mst.edu short choice; cout > 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 http://cs.mst.edu short choice; cout > choice; switch (choice) { case 1: cout << “Oink”; break; case 2: cout << “Bark”; cout << “Ruffruff”; break; case 3: cout << “Moooo” << endl; break; }

13 http://cs.mst.edu short choice; cout > choice; switch (choice) { case 1: cout << “Oink”; break; case 2: cout << “Bark”; cout << “Ruffruff”; break; case 3: cout << “Moooo” << endl; break; }

14 http://cs.mst.edu short choice; cout > choice; switch (choice) { case 1: cout << “Oink”; break; case 2: cout << “Bark”; cout << “Ruffruff”; break; case 3: cout << “Moooo” << endl; break; }

15 http://cs.mst.edu short choice; cout > choice; switch (choice) { case 1: cout << “Oink”; break; case 2: cout << “Bark”; cout << “Ruffruff”; break; case 3: cout << “Moooo” << endl; break; }

16 http://cs.mst.edu short choice; cout > choice; switch (choice) { case 1: cout << “Oink”; break; case 2: cout << “Bark”; cout << “Ruffruff”; break; case 3: cout << “Moooo” << endl; break; }

17 http://cs.mst.edu short choice; cout > choice; switch (choice) { case 1: cout << “Oink”; break; case 2: cout << “Bark”; cout << “Ruffruff”; break; case 3: cout << “Moooo” << endl; break; }

18 http://cs.mst.edu short choice; cout > choice; switch (choice) { case 1: cout << “Oink”; break; case 2: cout << “Bark”; cout << “Ruffruff”; break; case 3: cout << “Moooo” << endl; break; }

19 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu bool quit = false; char choice; do { cout > 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 http://cs.mst.edu long student_number; short student_number_index; cout > 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 http://cs.mst.edu End of Session


Download ppt "Switch-Case Statement. What is a switch-case?  The switch-case statement is really nothing more than a glorified."

Similar presentations


Ads by Google