Wednesday, February 10, 2010

In Java, The break statement is required in the default case of a switch selection structure. TRUE/ FALSE ?

FALSEIn Java, The break statement is required in the default case of a switch selection structure. TRUE/ FALSE ?
Absolutely not. Breaks are not required in switch statements. In fact, you can use multiple switch cases to run the same block of code just by stacking it above each other without breaks. The default case it no different. I use it anyway just because it puts me in the habit of adding breaks and it was recommended when I was reading a programming book.In Java, The break statement is required in the default case of a switch selection structure. TRUE/ FALSE ?
Yes the break statement is used in the default case. This is because whenever there is no match with the cases given, then only it will be handled by the default case and its like any other cases so break has to be used otherwise it will go in an indefinite loop within that default case...........






false. default is last case. but if you don't insert break in all other case, every switch case is executed.


switch( choice )


case 1: do something


break


case 2: do something


break


default


do something

No comments:

Post a Comment