by R4R Team

Why we require break statement in switch case ?

program-

#include< stdio.h>
int main()
{
int a=1;
switch(a)
{
case 1:
printf("case 1 is execute\n");
case 2:
printf("case 2 is execute\n");
case 3:
printf("case 3 is execute");
}
}


output-

case 1 is execute
case 2 is execute
case 3 is execute

-In this program, we not use the break so that in the switch case if any case is execute then by default it will execute all upcoming case.

Use of break in above code:
program-

#include< stdio.h>
int main()
{
int a=1;
switch(a)
{
case 1:
printf("case 1 is execute\n");
break;
case 2:
printf("case 2 is execute\n");
break;
case 3:
printf("case 3 is execute");
}
}


output-

case 1 is execute.

-In this program, we use the break in every case so that if any case is execute then we directly stop the switch and come outside the switch statement.
-For understanding the use of the break in switch statement, see the difference in both above output.




Leave a Comment: