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 !


Codzify's

1:1 & 1:Many Mentorship Program

Be job-ready. Learn how to crack the interviews with industry ready training program.


Learn More

Data Structures & Algorithms

What is Dynamic Programming and how to use it

This is a quick introduction to Dynamic Programming.

3 ways to check anagrams

Let's discuss the 3 ways to check anagrams and their time complexity.

Bubble Sort Algorithm

Topics covered: 1) Basics of Bubble Sort Algorithm 2) Worst Case, Best Case complexity

Introduction to Trees

What is a Tree and Terminologies like root, sibling, parent node, children, Leaf node, Ancestor & Descendant, Properties of Tree Data Structure, Depth of node, Height of node

Address Calculation in 2d array

Let's discuss about the Address Calculation using row-major order & column-major order.

Javascript

Javascript - Postfix Vs Prefix

Let's discuss the difference between postfix and prefix with a practical example.

Javascript Identifiers, Variables

In this session, we will discuss the concept of identifiers, variables and operators in javascript with a practical example.

Bootstrap

Bootstrap 4 Tutorial: Modal Popups Made Easy | Live Demo

Let's discuss the Modal Popups in Bootstrap.

How to create a dropdown list in navbar bootstrap

In this video, we will discuss How to create a dropdown list in navbar bootstrap with a practical example.

Navigation tabs in Bootstrap

Let's discuss about the navigation tabs in Bootstrap

Bootstrap Buttons

Let's discuss about the button classes in bootstrap.


Recommended tutorials