CODE
class BreakSwitch
{
public static void main(String arg[])
{
int rating = 1; // rating out of 4
switch(rating)
{
case 1:
System.out.println("Poor"); // LINE A
break;
case 2:
case 3:
System.out.println("Average"); // LINE B
break;
case 4:
System.out.println("Good"); // LINE C
break;
}
System.out.println("After switch"); // LINE D
}
}
Poor
After switch
Here rating
is 1
hence LINE A
and LINE D
are executed. When break
is encountered after LINE A
, the control directly goes to the next statement after switch
, which is LINE D
, hence prints After switch
.
When rating is 2 or 3, LINE B
and LINE D
are executed, when it is 4, LINE C
and LINE D
will be executed.
break
statements at LINE A
, LINE B
, LINE C
and see the output. The output will be as shown.rating
to float. It shows a compilation error, since switch
cannot function with float
, double
and long
data types.