Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now
Switch Statement in C: Syntax and Examples

Switch Statement in C: Syntax and Examples

18 Jul 2024
Beginner
5.01K Views
11 min read
Learn with an interactive course and practical hands-on labs

C Programming Free Course with Certificate: Learn C In 21 Days

Switch Statement in C

The switch statement is one of the most basic decision-making concepts in the C programming language. Switch statement is a control flow component that facilitates multi-way branching according to an expression's value. In C, the switch statement is composed of the switch keyword, an expression enclosed in parenthesis, and several case labels that specify potential values. C++ (CPP) also makes use of this statement for comparable reasons. You can study examples showing how switch statements are used in real-world C applications to gain a better understanding.

In this C tutorial, we'll explore what makes switch statements so versatile and practical, from how they're set up to how they can be used effectively. 

What is the Switch Statement in C?

In the previous conditional statements tutorial we saw the if else-if ladder to check multiple conditions if one or the other fails. The switch statement serves as an alternative to the if else-if ladder. It checks multiple cases for different values of a single variable.

switch statement in c programming

Syntax

The syntax of the switch statement in C is as follows:
switch(expression){ 
case value1: 
//code to be executed; 
break; //optional 
case value2: 
//code to be executed; 
break; //optional 
...... 
default: 
code to be executed if all cases are not matched; 
} 

The working of the switch the statement goes like this

  • It starts with the keyword switch, followed by a set of () containing an expression.
  • The expression is evaluated once and compared with the values of each case.
  • If any case matches, the corresponding case statement executes.
  • After the execution of the matching case statement, the control comes out of the switch statement due to the break keyword.
  • If no case matches, the default statement, if present, gets executed.

Read More - Top 50 Mostly Asked C Interview Questions and Answers

Rules for the switch statement in C

  1. The data type of the switch expression must be of an integer or character.
  2. All the case labels must be followed by a colon “:”.
  3. The case the value must be an integer or character constant.
  4. The break keyword is optional, but it can decrease the execution time of your program. It will stop the unnecessary execution of other cases after a match is found.

Note: We will learn break statement in detail in the section, Break and Continue Statements in C

Example


#include <stdio.h> 
int main(){ 
int number=100; 
switch(number){ 
case 10: 
printf("number is equals to 10"); 
break; 
case 50: 
printf("number is equal to 50"); 
break; 
case 100: 
printf("number is equal to 100"); 
break; 
default: 
printf("number is not equal to 10, 50 or 100"); 
} 
return 0; 
} 

The above code in the C Compiler checks if thenumber is 10 or is it 50 or is it 100. If none of the given numbers exist, the default the statement gets printed.

Output

number is equal to 100

Nested Switch Case in C

There can be several switch statements inside a switch statement. These are called nested switch statements.

Syntax

switch (expression1)
{
 case value1:
 // Code block for value1
 switch (expression2)
 {
 case valueA:
 // Code block for valueA
 break;
 case valueB:
 // Code block for valueB
 break;
 // More cases for expression2
 }
 break;
 case value2:
 // Code block for value2
 break;
 // More cases for expression1
 default:
 // Default code block for expression1
}

Example


#include <stdio.h> 
int main () { 
 int i = 10; 
 int j = 20; 
 switch(i) { 
 case 10: 
 printf("the value of i evaluated in outer switch: %d\n",i); 
 case 20: 
 switch(j) { 
 case 20: 
 printf("The value of j evaluated in nested switch: %d\n",j); 
 } 
 } 
 printf("Exact value of i is : %d\n", i ); 
 printf("Exact value of j is : %d\n", j ); 
 return 0; 
} 
  • In the above code, the outer switch statement checks the value of i.
  • The value of i matches with the case 10. So, the corresponding statement gets printed.
  • Since there's no break statement, it continues to execute the code in the subsequent case 20 block.
  • Inside the case 20 block, there is a nested switch statement that checks the value of j.
  • The value of j matches with  case 20 in the nested switch block. So, the corresponding statement gets printed.

Output

the value of i evaluated in the outer switch: 10
The value of j evaluated in nested switch: 20
Exact value of i is : 10
Exact value of j is : 20
Summary

In conclusion, switch statements in C provide a useful alternative to the traditional “if” statement for making decisions within your code. Not only are they more efficient and precise, but they also provide many advantages. As you continue learning about the intricacies of switch statements in C, remember the tremendous power it gives you not only to control what is inside your program’s box of instructions but also how best to apply those instructions for maximum results. C Online Course Certification will help you more in your understanding of switch statements and other essential concepts.

FAQs

The switch expression must be of an integer or character type. The case value must be an integer or character constant. The case value can be used only inside the switch statement. The break statement in switch case is not a must.

The value of the expressions in a switch-case statement must be an ordinal type i.e. integer, char, short, long, etc.

Switch statements can only contain return statements if they are within an object that accepts a callback. T
Share Article
About Author
Sakshi Dhameja (Author and Mentor)

She is passionate about different technologies like JavaScript, React, HTML, CSS, Node.js etc. and likes to share knowledge with the developer community. She holds strong learning skills in keeping herself updated with the changing technologies in her area as well as other technologies like Core Java, Python and Cloud.

Accept cookies & close this