Using switch Statements in C++
By: Emiley J Printer Friendly Format
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; break; case valueTwo: statement; break; .... case valueN: statement; break; 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 3: 4: #include <iostream.h> 5: 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 Excellent! Masterful! Incredible! 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; break case 1: cout << "One!" << endl; break; case 2: cout << "Two!" << endl; default: cout << "Default!" << endl; }
Example 2
switch (choice) { choice 0: choice 1: choice 2: cout << "Less than 3!"; break; choice 3: cout << "Equals 3!"; break; default: cout << "greater than 3!"; }
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
Calculating total based on the given quantity and price in C++
Sorting an array of Strings in C++
Matrix using nested for loops in C++
Compute the square root of the sum of the squares of an array in C++
Calculate average using Two-Dimensional Array in C++
Two-Dimensional Array Manipulation in C++
Compiling and Linking Multiple Source Files in C++
Escape Sequences for Nonprintable Characters in C++
Using the Built-in Arithmetic Types in C++
Archived Comments
1. thanks or the infromation about switch statement
View Tutorial By: mikko at 2008-09-01 23:48:44
2. I've been satisfied with your tutorial site. Since
View Tutorial By: Mark Harold F. Manguino at 2008-08-06 05:00:54
3. can you give the program about the fast food by us
View Tutorial By: grace at 2008-09-22 00:48:37
4. can u help me????
View Tutorial By: shadowlast at 2008-09-30 00:58:04
5. thank you so much i learn from your tutorial i wis
View Tutorial By: Anonymous at 2009-01-03 20:08:41
6. thankx a lot...i've been trying so hard to learn t
View Tutorial By: bhuwan at 2009-04-08 23:30:45
7. all of codes of t-C++ ide, it is applicable to use
View Tutorial By: jonard at 2009-06-05 20:41:39
8. i like your explanation it is understandable
View Tutorial By: gerasmia yen-yen d. at 2009-07-15 21:12:39
9. ITS GOOD TO UNDERSTAND AS A BEGINNER,
THANK
View Tutorial By: Anonymous at 2009-07-24 06:31:50
10. its nice way of explanation
View Tutorial By: soniya at 2009-08-10 00:38:05
11. thanks for the information about switch statement
View Tutorial By: queenie at 2009-08-11 05:26:53
12. your tutorial is very helpful...
i was hopi
View Tutorial By: Zero17 at 2009-08-15 09:17:16
13. i always encounter a problem every time we discuss
View Tutorial By: sukira 18 at 2009-08-29 03:25:23
14. Wow.
This is word for word right ou
View Tutorial By: James at 2009-11-13 18:45:52
15. Thanks for the syntax, this has really helped me u
View Tutorial By: Ketobbey at 2010-04-30 19:01:29
16. C++ is more than nyc & fun, de most interestin
View Tutorial By: Themba at 2010-09-12 09:11:04
17. thank you so much i satisfied for the tutorial in
View Tutorial By: flick at 2010-09-24 21:10:59
18. tbang sa project
View Tutorial By: mikai at 2010-09-27 18:36:36
19. nice information about switch cases
View Tutorial By: sunil at 2010-12-11 07:38:16
20. good....useful...thank you
View Tutorial By: Aarcha at 2011-01-28 20:31:38
21. I want to ask, can a formula be inserted in this s
View Tutorial By: chika at 2011-02-02 17:18:59
22. I want inserted switch statement in my program,
View Tutorial By: shah at 2011-02-07 01:11:12
23. Hi,
What should be done to correct the erro
View Tutorial By: Raymond at 2011-03-07 08:51:54
24. thanks a lot because of this example my program w
View Tutorial By: ROBERT CABRAS] at 2011-03-07 20:59:42
25. thank you
i memorized it easly .
View Tutorial By: kibrom at 2011-04-27 11:46:24
26. thanks for our help. god bless u and again thanks.
View Tutorial By: mehryab at 2011-05-08 14:44:21
27. can we use if statment with swich,,,!!!
View Tutorial By: dena at 2011-07-09 13:52:27
28. please give me a program of the 12 horoscope using
View Tutorial By: auvx at 2011-08-03 02:02:56
29. Thanks a lot for your tutorial.
(: It was v
View Tutorial By: Nevermore at 2011-08-03 10:39:32
30. 3. Write a program that will continuously ask for
View Tutorial By: basil at 2011-08-04 13:00:30
31. I LIKE THE EXPLANATION.. HOPE I CAN .UNDERSTAND MO
View Tutorial By: JUNER at 2011-08-17 03:29:16
32. CAN I ASK A FORMULA OF FOR MAKING A CALCULATOR USI
View Tutorial By: JUNER at 2011-08-17 03:33:02
33. can i ask a program that will compute a order from
View Tutorial By: mjae at 2011-09-12 08:02:46
34. the tutorial is good and i am satisfied to it i wa
View Tutorial By: sheraz at 2011-11-07 02:08:55
35. thank's for the info.. i've learned a lot.. now, i
View Tutorial By: farah talabo at 2012-01-08 23:55:14
36. what if i use multiple switch statement, is it wor
View Tutorial By: earl at 2012-01-29 04:10:25
37. so chalenging in c++,medyo nahihirapan ako kaya in
View Tutorial By: valiery :) at 2012-02-15 02:03:43
38. program ko continioue rakhne ke liye kya karna pad
View Tutorial By: imran at 2012-02-21 17:47:42
39. Thanks ;)
View Tutorial By: Lelouch at 2012-02-24 12:59:39
40. you can use multiple switch statements depending o
View Tutorial By: moshood at 2012-04-01 13:24:41
41. its very useful but its not working on compiler
View Tutorial By: a at 2012-04-18 11:23:38
42. thanx a lot!!!
View Tutorial By: navjot at 2012-05-18 09:07:01
43. can I ask question? how to make a complete program
View Tutorial By: manuel at 2012-08-13 10:06:24
44. i need a program and the output is like a ATM mach
View Tutorial By: Akira at 2012-08-26 11:26:10
45. can i ask question? on how to make a statement by
View Tutorial By: alas at 2012-09-27 00:42:08
46. this has helped me in a million ways...thanx hugge
View Tutorial By: rasheed at 2012-09-27 07:35:56