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.
3 Respostas
+ 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.
+ 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;
}
0
The best time to use break is when you want to control flow from the switch statement to the main program.