The problem or error in the previous program can be corrected by making use of the break statement.
Break statement can be used, in either a repetition structure or a selection to exit from the structure.
The following program is the previous program with the break statement.
Example 1:
Sample program, using break statement
#include<stdio.h>
int main(void)
{
int i=1;
switch(i);
{
case 1:
printf(“I am in case 1”);
break;
case 2:
printf(“This is second one”);
break;
case 3:
printf(“This is third case”);
break;
default:
printf(“you are in default”);
}
return 0;
}
OUTPUT
I am in case 1
Example 2:
Sample program, using switch statement
#include<stdio.h>
int main(void)
{
int i=4;
switch(i)
{
default:
printf(“\n A mouse is an alphabet built by the japanese”);
case 1:
printf(“\nBreeding rabbits is a good experience”);
break;
case 2:
printf(“Friction is a drag”);
break;
case 3:
printf(“\n If practice make perfect, then nobody is perfect”);
}
return 0;
}
OUTPUT
A mouse is an alphabet built by Japanese
Breeding rabbits is a raising experience
Example 3:
Sample program, using switch statement
#include<stdio.h>
int main(void)
{
int i=4,j=2;
switch(i)
{
case 1:
printf(“\n To err is human, to forgive is against company policy”);
break;
case j:
printf(“\n If you have nothing to do, don’t do it here”);
break;
}
Return 0;
}
OUTPUT
Error. Constant expression required, you cannot use variable j