Switch Statement in C Language - Codzify Tutorials

4 min 30 sec read Basic


The control statement that allows us to make a decision from the number of choices is called a switch, or more correctly a switch- case-default, since these three keywords go together to make up the control statement.

Syntax of switch-case Statements :-

switch (expression) {
case expression :
      do this ;
      break;
 case expression : 
     do this ; break;
 case expression :
     do this ;
     break;
default :
      this ;
}

switch statements can accepts 'integer constants' or 'Character variables'.

What happens during the execution of switch statements ?

Firstly , expresssion under switch keyword inside paranthesis is evaluated .Then each case is evaluated one by one unless and until match is found. If no match is found , default case is being executed.

But why break statement ?

Once matched case is found why to execute rest cases. It is better to write break inside each case so that once needed case is found, program execution goes outside of switch brackets.

Example (Program without break statement)

/* Copyright @codzify 2017 */
#import <stdio.h>
#import <conio.h>

void main( ) {
int i = 2 ;
switch ( i ) {
case 1 :
   printf ( "I am in case 1 \n" ) ;
case 2 :
    printf ( "I am in case 2 \n" ) ;
case 3 :
    printf ( "I am in case 3 \n" ) ;
default :
   printf ( "I am in default \n" ) ;
} 
}

Output

I am in case 2
I am in case 3
I am in default

As you see , 'i' gets assigned with value 2. Then expression under switch statement is being evaluated. Then, switch will search for case 2 and executes printf statement then it will execute next case (case 3) also because we did not provided break statement in case 2. Then in case 3 it will executes "I am in case 3" statement and as there is also no break statement default case will gets executed.

Example (Program with break statement)

/* Copyright @codzify 2017 */
#import <stdio.h>
#import <conio.h>

void main( ) {
int i = 2 ;
switch ( i ) {
case 1 :
   printf ( "I am in case 1 \n" ) ;
   break;
case 2 :
    printf ( "I am in case 2 \n" ) ;
    break;
case 3 :
    printf ( "I am in case 3 \n" ) ;
    break;
default :
   printf ( "I am in default \n" ) ;
   break;
} 
}

Output

I am in case 2

Switch Statements in case of character variables

/* Copyright @codzify 2017 */
#import <stdio.h>
#import <conio.h>

void main( ) {
char ch ;
printf ("Enter any of the alphabet a, b, or c ") ; 
scanf ("%c", &ch ) ;

switch ( ch ) 
{
case 'a' :
case 'A' :
   printf ( "print a or A" ) ;
   break ; 

case 'b' :
case 'B' :
    printf ( "b or B" ) ; 
    break ;
case 'c' : 
case 'C' :
    printf ( "c or C" ) ;
    break ; 

default :
    printf ( "Print Defualt Statements" ) ; }
}

Here we will take input from user . Suppose user entered b Then b or B statements gets printed

Input :-

Enter any of the alphabet a, b, or c
 b

Output :-

b or B

Hope you got better understanding of the switch statement in C. In next tutorial, you will learn about pointers in C language. Happy Learning !


How was the tutorial? Nice. Right?



Deep Concept videos to crack the highly-piad interviews.

Chekout out our Youtube Channel to get detailed video content on important topics in interviews.



What is your Interview Score?

Test your skillset with the curated questions created by experts around the globe.

Book a free test slot. Now !.

It usually takes 30 mins for an online test and this test will be MCQ based. Get detailed analytics based on your test and recommedations with personalised roadmaps.

Book a test slot. Now!

Have questions about the trial coding class?

Chat with our experts to discuss

Connect on WhatsApp


Recommended tutorials

#online compiler for c     #python for programming    

#dfs and bfs algorithm     #programming with c language

#storageclass in C    #listcomprehension in python