0
How do switch works in java?
just cant get the breaks and defaults and stuffs
2 ответов
+ 1
int day=3;
switch(day){
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tueday");
break;
default:
System.out.println("Ey");
}
default is used when the value of the varibale is not found in the switch like the switch above. The switch only has case 1 and 2 but because day=3, the switch needs a case 3 but because it doesnt have one, the default is used.
The break is used to break the flow. If i added a case 3 and said day =3 .Then after that, i take the break away from case 3, the output would be "Wednesday" and "ey" because the loop doesnt know that once it gets the case it should break away from the loop and not print the values that come after it as well.
+ 1
tnx for the info Mogammad Shameer Losper