+ 1
Help- switch statements and breaks
I have just learned in Java about "switch". My question is, why do we need the "break" line? The flow ends anyway when the curly brace appears and ends this statement... doesn't it?
4 ответов
+ 9
If you don't use break statement then the program executes till end and all statements below the required statement would also be executed.
+ 7
It allows fall through the condition so you don't need to repeat the statements.
For example if 2 different cases lead to the same result, you don't need to write break for the 1st case and let it flows to the statement in 2nd case.
💡 Demo
https://code.sololearn.com/c71KtCEf8yCQ/?ref=app
+ 1
thank you!
+ 1
break it till you make it ;)