Using switch Statements in C++

By: Emiley J  

if and if/else statements can become quite confusing when nested too deeply, and C++ offers an alternative. Unlike if, which evaluates one value, switch statements allow you to branch on any of a number of different values. The general form of the switch statement is:

switch (expression)
case valueOne: statement;
case valueTwo: statement;
case valueN:   statement;
default:       statement;

expression is any legal C++ expression, and the statements are any legal C++ statements or block of statements. switch evaluates expression and compares the result to each of the case values. Note, however, that the evaluation is only for equality; relational operators may not be used here, nor can Boolean operations.

If one of the case values matches the expression, execution jumps to those statements and continues to the end of the switch block, unless a break statement is encountered. If nothing matches, execution branches to the optional default statement. If there is no default and there is no matching value, execution falls through the switch statement and the statement ends.


NOTE: It is almost always a good idea to have a default case in switch statements. If you have no other need for the default, use it to test for the supposedly impossible case, and print out an error message; this can be a tremendous aid in debugging.

It is important to note that if there is no break statement at the end of a case statement, execution will fall through to the next case statement. This is sometimes necessary, but usually is an error. If you decide to let execution fall through, be sure to put a comment, indicating that you didn't just forget the break.

This C++ program illustrates use of the switch statement.

Demonstrating the switch statement.

1:  //
2:  // Demonstrates switch statement
4:  #include <iostream.h>
6:  int main()
7:  {
8:    unsigned short int number;
9:    cout << "Enter a number between 1 and 5: ";
10:    cin >> number;
11:    switch (number)
12:    {
13:       case 0:   cout << "Too small, sorry!";
14:                 break;
15:       case 5:  cout << "Good job!\n";  // fall through
16:       case 4:  cout << "Nice Pick!\n"; // fall through
17:       case 3:  cout << "Excellent!\n"; // fall through
18:       case 2:  cout << "Masterful!\n"; // fall through
19:       case 1:  cout << "Incredible!\n";
20:                break;
21:       default: cout << "Too large!\n";
22:                break;
23:    }
24:    cout << "\n\n";
25:     return 0;
26: }

Output: Enter a number between 1 and 5: 3

Enter a number between 1 and 5: 8
Too large!

Analysis: The user is prompted for a number. That number is given to the switch statement. If the number is 0, the case statement on line 13 matches, the message Too small, sorry! is printed, and the break statement ends the switch. If the value is 5, execution switches to line 15 where a message is printed, and then falls through to line 16, another message is printed, and so forth until hitting the break on line 20.

The net effect of these statements is that for a number between 1 and 5, that many messages are printed. If the value of number is not 0-5, it is assumed to be too large, and the default statement is invoked on line 21.

The switch Statement

The syntax for the switch statement is as follows:

switch (expression)
case valueOne: statement;
case valueTwo: statement;
case valueN: statement
default: statement;

The switch statement allows for branching on multiple values of expression. The expression is evaluated, and if it matches any of the case values, execution jumps to that line. Execution continues until either the end of the switch statement or a break statement is encountered. If expression does not match any of the case statements, and if there is a default statement, execution switches to the default statement, otherwise the switch statement ends.

Example 1

switch (choice)
case 0:
        cout << "Zero!" << endl;
case 1:
        cout << "One!" << endl;
case 2:
       cout << "Two!" << endl;
       cout << "Default!" << endl;

Example 2

switch (choice)
choice 0:
choice 1:
choice 2:
       cout << "Less than 3!";
choice 3:
       cout << "Equals 3!";
       cout << "greater than 3!";

Archived Comments

1. this has helped me in a million ways...thanx huggest guyz!!
View Tutorial          By: rasheed at 2012-09-27 07:35:56

2. can i ask question? on how to make a statement by using a if else statement and switch statement
View Tutorial          By: alas at 2012-09-27 00:42:08

3. i need a program and the output is like a ATM machine....
can u give me a sample?

View Tutorial          By: Akira at 2012-08-26 11:26:10

4. can I ask question? how to make a complete program to simulate the game jackNpoy? thank you
View Tutorial          By: manuel at 2012-08-13 10:06:24

5. thanx a lot!!!
View Tutorial          By: navjot at 2012-05-18 09:07:01

6. its very useful but its not working on compiler
View Tutorial          By: a at 2012-04-18 11:23:38

7. you can use multiple switch statements depending on your program, and when there is more than one sw
View Tutorial          By: moshood at 2012-04-01 13:24:41

8. Thanks ;)
View Tutorial          By: Lelouch at 2012-02-24 12:59:39

9. program ko continioue rakhne ke liye kya karna padega....example dekar bataiye pls sir.....
View Tutorial          By: imran at 2012-02-21 17:47:42

10. so chalenging in c++,medyo nahihirapan ako kaya incode muna ng incode......i wana ask something, pan
View Tutorial          By: valiery :) at 2012-02-15 02:03:43

11. what if i use multiple switch statement, is it works?
what is the code? can u help! please!

View Tutorial          By: earl at 2012-01-29 04:10:25

12. thank's for the info.. i've learned a lot.. now, i better understand the essence of switch statement
View Tutorial          By: farah talabo at 2012-01-08 23:55:14

13. the tutorial is good and i am satisfied to it i want to learn how to use calender although i made it
View Tutorial          By: sheraz at 2011-11-07 02:08:55

14. can i ask a program that will compute a order from costumer using switch and do while statement in C
View Tutorial          By: mjae at 2011-09-12 08:02:46

View Tutorial          By: JUNER at 2011-08-17 03:33:02

View Tutorial          By: JUNER at 2011-08-17 03:29:16

17. 3. Write a program that will continuously ask for an integer input from the user until a negative
View Tutorial          By: basil at 2011-08-04 13:00:30

18. Thanks a lot for your tutorial.
(: It was very helpful and well-conceived.
And I just

View Tutorial          By: Nevermore at 2011-08-03 10:39:32

19. please give me a program of the 12 horoscope using the switch statement in functions.
View Tutorial          By: auvx at 2011-08-03 02:02:56

20. can we use if statment with swich,,,!!!
View Tutorial          By: dena at 2011-07-09 13:52:27

21. thanks for our help. god bless u and again thanks.
View Tutorial          By: mehryab at 2011-05-08 14:44:21

22. thank you
i memorized it easly .

View Tutorial          By: kibrom at 2011-04-27 11:46:24

23. thanks a lot because of this example my program well done successful.
View Tutorial          By: ROBERT CABRAS] at 2011-03-07 20:59:42

24. Hi,
What should be done to correct the error, just in case instead of a number, the user ente

View Tutorial          By: Raymond at 2011-03-07 08:51:54

25. I want inserted switch statement in my program,
Really need your help and comment.. Thank yo

View Tutorial          By: shah at 2011-02-07 01:11:12

26. I want to ask, can a formula be inserted in this switch statement like the one I provided below?? Re
View Tutorial          By: chika at 2011-02-02 17:18:59

27. good....useful...thank you
View Tutorial          By: Aarcha at 2011-01-28 20:31:38

28. nice information about switch cases
View Tutorial          By: sunil at 2010-12-11 07:38:16

29. tbang sa project
View Tutorial          By: mikai at 2010-09-27 18:36:36

30. thank you so much i satisfied for the tutorial in c++..i don't have a problem when you are always gi
View Tutorial          By: flick at 2010-09-24 21:10:59

31. C++ is more than nyc & fun, de most interesting part abt C++ is knowing de algorithim of Ur code
View Tutorial          By: Themba at 2010-09-12 09:11:04

32. Thanks for the syntax, this has really helped me understand how to use 'switch'.
View Tutorial          By: Ketobbey at 2010-04-30 19:01:29

33. Wow.

This is word for word right out of "teach yourself C++ in 24 hours".

View Tutorial          By: James at 2009-11-13 18:45:52

34. i always encounter a problem every time we discuss new things in my C++ subject.. i don't know if i'
View Tutorial          By: sukira 18 at 2009-08-29 03:25:23

35. your tutorial is very helpful...
i was hoping that you give more example of that within 30-50

View Tutorial          By: Zero17 at 2009-08-15 09:17:16

36. thanks for the information about switch statement
View Tutorial          By: queenie at 2009-08-11 05:26:53

37. its nice way of explanation
View Tutorial          By: soniya at 2009-08-10 00:38:05


View Tutorial          By: Anonymous at 2009-07-24 06:31:50

39. i like your explanation it is understandable
View Tutorial          By: gerasmia yen-yen d. at 2009-07-15 21:12:39

40. all of codes of t-C++ ide, it is applicable to use to the other C++..?like java?,devc++,and others?
View Tutorial          By: jonard at 2009-06-05 20:41:39

41. thankx a lot...i've been trying so hard to learn the switch statement ..... this tutorial shined my
View Tutorial          By: bhuwan at 2009-04-08 23:30:45

42. thank you so much i learn from your tutorial i wish ill be expert in programming like you.can you gi
View Tutorial          By: Anonymous at 2009-01-03 20:08:41

43. can u help me????
View Tutorial          By: shadowlast at 2008-09-30 00:58:04

44. can you give the program about the fast food by using the switch statement.... pls.....
View Tutorial          By: grace at 2008-09-22 00:48:37

45. thanks or the infromation about switch statement
View Tutorial          By: mikko at 2008-09-01 23:48:44

46. I've been satisfied with your tutorial site. Since I started to learn java and C++, I find it diffic
View Tutorial          By: Mark Harold F. Manguino at 2008-08-06 05:00:54

Most Viewed Articles (in C++ )

Latest Articles (in C++)

Comment on this tutorial