-1

I understand that switch statements will run all the code after the first true case, but why does it execute every case after the true case?

1 Answers1

-3

You need to break; after executing the case code block if you do not want to check the other switch conditions after a matching condition is found.

Mish
  • 145
  • 5
  • Question isn't *how* to break it, but more like *why* do we need to break it? – Pshemo Oct 20 '17 at 15:39
  • The question is why does it continue executing the rest of the switch after finding a true case. – Mish Oct 20 '17 at 15:41
  • Yes, and does "You need to break; ..." answer it? Question is more about design decision about *why* switch-case work as it work, not *how* to use it. – Pshemo Oct 20 '17 at 15:45