0

What is exactly the use of break in a switch statement?

I mean like when do we use it and when is no break needed.

28th Jan 2017, 9:54 AM
May
May - avatar
3 ответов
+ 2
You will need a break on each case, it's like telling your case switch to stop going trough the cases because you found what you were looking for. You can't have a case switch without breaks.
28th Jan 2017, 10:08 AM
Alex
Alex - avatar
+ 1
You should use break when you want to leave the switch statement. This means that you can have multiple cases execute before leaving the switch statement, or just one. example: int i = 3; switch(i) { case 0: case 1: case 2: println("Less than 3.); break; case 3: println("Equals 3."); break; default: break; }
28th Jan 2017, 10:09 AM
Sean
0
The best time to use break is when you want to control flow from the switch statement to the main program.
8th Feb 2017, 12:31 PM
Isaac Essuman
Isaac Essuman - avatar