Help- switch statements and breaks | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

10th Sep 2017, 1:22 PM
‎‏‪Liran Sharabi
4 Answers
+ 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.
10th Sep 2017, 1:37 PM
Lakshay
Lakshay - avatar
+ 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
10th Sep 2017, 3:19 PM
Zephyr Koo
Zephyr Koo - avatar
+ 1
thank you!
10th Sep 2017, 1:40 PM
‎‏‪Liran Sharabi
+ 1
break it till you make it ;)
11th Sep 2017, 3:34 PM
Daniel Gaishuber
Daniel Gaishuber - avatar