0
After break we can add an optional statement??why??
can someone give me an example??? plz ......thx
3 odpowiedzi
+ 7
In Java, switch is use when you may have different code to run depending on a single thing(value/variable/etc).
Example:
//accept action(int) as input from user
switch (action) {
case 0:
//go to bath
break;
case 1:
//go to school
break;
case 2:
//go to sleep
break;
default:
//continue coding
}
If there is no break statement between the option(cases), there will be a fall-through in the code, where in this case, the person will go to bath, then go to school, then go to sleep and later continue coding.
Therefore, break statement is important to let the compiler knows where the code should stop or break.
PS: If I am not mistaken, the default case can ignore the break, as nothing below it to be fall-through.
+ 4
That would really be of no use if its inside loop, and is after break
0
thx