3 ms·
You're thinking about C's switch statement the wrong way. You are thinking about where a value is used to determine which block of code to run, like a ramped u
by jnordwick 7y ago
You're thinking about C's switch statement the wrong way.
You are thinking about where a value is used to determine which block of code to run, like a ramped up conditional or an if-else-if chain.
C's switch is a more a complex goto statement. All the case statements are labels and the switch does a goto label for the correct value. Those labels just need to be inside the switch block. This is why you can switch into the the middle of a looping construct or if-statement. In this way, break is kind of the oddity and sugar for goto past end of the switch block. This allows for some complex control flow that is kind of useful in a language without much else to offer in that regard (besides the ugly setjump hackiness).