program to illustrate the working of switch in the presence of break statements ?
Answers
Import
java.util.scanner;
Public class main
{
Public static void main(string args[])
{
Scanner sc =new scanner (system.in);
Int age;
age=7:
switch(age)
{
Case1:
system.out.println("you are crawl");
break;
case2:
system.out.println("you can get in trouble");
break;
default:
system.out.println("I donot know how old you are");
break;
}
}
}
Regards
rishi Arya .co
Switch statement: It is one of decision-making control flow statements.
Now, I am gonna illustrate the switch case with and without break statements.
(i)
Switch With break statements: - Default one:
#include<stdio.h>
int main()
{
int a = 2;
switch(a)
{
case 0:
printf("A");
break;
case 1:
printf("B");
break;
case 2:
printf("C");
break;
default:
printf("D");
break;
}
return 0;
}
Output: C
Reason: Here, i have used break statement to break the flow of control of every case block.
-------------------------------------------------------------------------------------------------------------
(ii)
Switch without break statements:
#include<stdio.h>
int main()
{
int a = 2;
switch(a);
{
case 0:
printf("A");
case 1:
printf("B");
case 2:
printf("C");
default:
printf"(D");
}
return 0;
}
Output : CD
Reason: Here, with the absence of break statement, all the statements below the matched case gets executed.
Hope this helps!