0

After break we can add an optional statement??why??

can someone give me an example??? plz ......thx

28th Mar 2017, 4:05 PM
abhijit mishra
abhijit mishra - avatar
3 Antworten
+ 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.
28th Mar 2017, 4:54 PM
Heng Jun Xi
Heng Jun Xi - avatar
+ 4
That would really be of no use if its inside loop, and is after break
28th Mar 2017, 4:27 PM
Meharban Singh
Meharban Singh - avatar
0
thx
28th Mar 2017, 6:37 PM
abhijit mishra
abhijit mishra - avatar