C – switch case statement in C Programming with example (2024)

The switch case statement is used when we have multiple options and we need to perform a different task for each option.

C – Switch Case Statement

Before we see how a switch case statement works in a C program, let’s checkout the syntax of it.

switch (variable or an integer expression){ case constant: //C Statements ; case constant: //C Statements ; default: //C Statements ;}

Flow Diagram of Switch Case

C – switch case statement in C Programming with example (1)

Example of Switch Case in C

Let’s take a simple example to understand the working of a switch case statement in C program.

#include <stdio.h>int main(){ int num=2; switch(num+2) { case 1: printf("Case1: Value is: %d", num); case 2: printf("Case1: Value is: %d", num); case 3: printf("Case1: Value is: %d", num); default: printf("Default: Value is: %d", num); } return 0;}

Output:

Default: value is: 2

Explanation: In switch I gave an expression, you can give variable also. I gave num+2, where num value is 2 and after addition the expression resulted 4. Since there is no case defined with value 4 the default case is executed.

Twist in a story – Introducing Break statement

Before we discuss more about break statement, guess the output of this C program.

#include <stdio.h>int main(){ int i=2; switch (i) { case 1: printf("Case1 "); case 2: printf("Case2 "); case 3: printf("Case3 "); case 4: printf("Case4 "); default: printf("Default "); } return 0;}

Output:

Case2 Case3 Case4 Default

I passed a variable to switch, the value of the variable is 2 so the control jumped to the case 2, However there are no such statements in the above program which could break the flow after the execution of case 2. That’s the reason after case 2, all the subsequent cases and default statements got executed.

How to avoid this situation?
We can use break statement to break the flow of control after every case block.

Break statement in Switch Case

Break statements are useful when you want your program-flow to come out of the switch body. Whenever a break statement is encountered in the switch body, the control comes out of the switch case statement.

Example of Switch Case with break
I’m taking the same above that we have seen above but this time we are using break.

#include <stdio.h>int main(){ int i=2; switch (i) { case 1: printf("Case1 "); break; case 2: printf("Case2 "); break; case 3: printf("Case3 "); break; case 4: printf("Case4 "); break; default: printf("Default "); } return 0;}

Output:

Case 2

Whydidn’tI use break statement after default?
The control would itself come out of the switch after default so I didn’t use it, however if you want to use the break after default then you can use it, there is no harm in doing that.

Few Important points regarding Switch Case

1) Case doesn’t always need to have order 1, 2, 3 and so on. They can have any integer value after case keyword. Also, case doesn’t need to be in an ascending order always, you can specify them in any order as per the need of the program.

2) You can also use characters in switch case. for example –

#include <stdio.h>int main(){ char ch='b'; switch (ch) { case 'd': printf("CaseD "); break; case 'b': printf("CaseB"); break; case 'c': printf("CaseC"); break; case 'z': printf("CaseZ "); break; default: printf("Default "); } return 0;}

Output:

CaseB

3) The expression provided in the switch should result in a constant value otherwise it would not be valid.
For example:
Valid expressions for switch –

switch(1+2+23)switch(1*2+3%4)

Invalid switch expressions –

switch(ab+cd)switch(a+b+c)

4) Nesting of switch statements are allowed, which means you can have switch statements inside another switch. However nested switch statements should be avoided as it makes program more complex and less readable.
5) Duplicate case values are not allowed. For example, the following program is incorrect:
This program is wrong because we have two case ‘A’ here which is wrong as we cannot have duplicate case values.

#include <stdio.h>int main(){ char ch='B'; switch (ch) { case 'A': printf("CaseA"); break; case 'A': printf("CaseA"); break; case 'B': printf("CaseB"); break; case 'C': printf("CaseC "); break; default: printf("Default "); } return 0;}

6) The default statement is optional, if you don’t have a default in the program, it would run just fine without any issues. However it is a good practice to have a default statement so that the default executes if no case is matched. This is especially useful when we are taking input from user for the case choices, since user can sometime enter wrong value, we can remind the user with a proper error message that we can set in the default statement.

❮ PreviousNext ❯

Top Related Articles:

  1. C – while loop in C programming with example
  2. If statement in C programming with example
  3. C – loops in C programming with examples
  4. C – Pointer to Pointer (Double Pointer) with example
  5. User-defined function in C with Examples
C – switch case statement in C Programming with example (2024)
Top Articles
Latest Posts
Article information

Author: Dong Thiel

Last Updated:

Views: 5488

Rating: 4.9 / 5 (59 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.