Test your Programming knowledge

50% users failed to get the best score. It's your turn to test now.
Questions curated by the expert mentors at codzify.com

Start Quiz

Article

Switch Statement in C Language - Codzify Tutorials

4 min 30 sec read

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 !


Recommended Articles

Did you found this article helpful?

Try to execute what you have learnt

Easy to use online data structure compiler where you can execute the programs in your favourite programming language.
(C, C++, Python)

Open Compiler

HTML, CSS and Javascript Real time Web Editor

Execute your HTML, CSS and javascript code in real time with the web editor
(HTML, CSS, Bootstrap, Javascript)

Open Web Editor