Why break; is used in The switch statement | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why break; is used in The switch statement

Why it is said that a switch statement is equal to 3 if statements and why there is a break rather than continuing the code without break

2nd Sep 2022, 6:58 AM
Shaheer Shah
Shaheer Shah - avatar
1 Answer
+ 3
At some point, a switch statement is marginally more efficient than a series of if statements because it computes the jump point up front, versus going through many conditionals. But more importantly, switch is considered easier to read than a long chain of if statements. Leave out the break at the end of a case only if you want the execution to proceed into the code of the next case label. Regardless of the next case value, it will continue executing into the next case unless you command it to break out of the code block. There are a few times when it is desirable to continue through, but for most common uses of switch, the break would be needed.
2nd Sep 2022, 8:30 AM
Brian
Brian - avatar