Switch Case in C Language

 INTRODUCTION

 The switch statement allows to execute one code block among many alternatives. Switch case and if-else ladder are similar but the syntax of the switch statement is much easier to read and write. Also it used to perform different actions based on different conditions(cases).

Syntax   :

int main()

{

switch(ch)

{

case 1:

{

break;      ....................................it is used to jump out of a switch statement

}

case 2 :

{

break;

}

case 3 :

{

break;

}

default :

{

}

}

                                                                       Flowchart

Example :

#include<stdio.h>

int main()

{

int day = 4;

switch (day) 

{
case 1:

{
printf("Monday");
break;

}
case 2:
printf("Tuesday");
break;

}
case 3:

{
printf("Wednesday");
break;

}
case 4:

{
printf("Thursday");
break;

}
case 5:

{

printf("Friday");

break;

}
case 6:

{
printf("Saturday");
break;

}
case 7:

{
printf("Sunday");
break;

}

default:

{

printf("not valid");

}

}

output : Thursday


No comments:

Post a Comment