3 ms·
the break keyword in switch statements also allows fallthrough, allowing for a terser way to write state machines, at the cost of making the whole construct les
by wongarsu 8y ago
the break keyword in switch statements also allows fallthrough, allowing for a terser way to write state machines, at the cost of making the whole construct less intuitive and readable.
In a switch expression of course that is moot since break is a statement, and statements can't appear in expressions.
- kibwen 8y agoThe thrust of my above comment is largely about how the idea "enabling fallthrough" demonstrates how poorly the switch statement is usually taught: most people have an intuitive grasp of it as an alternative to if/else if, when in truth it's sugar for a jump table. Break exists to prevent fallthrough rather than enable it! And indeed fallthrough isn't useless, though decades of long experience has demonstrated that it is the wrong default, and newer languages like Go do better by making break the default and having a dedicated fallthrough keyword for when you want it.
- zamalek 8y agoC# doesn't allow fallthrough. Every branch in a case clause must exit the case clause. Fallthrough can only be aachieved with a goto at the end of the case clause. The `break;` requirement is completely superficial; there's no reason why it couldn't be a classical block/statement choice: case "foo" { /* ... */ } case "bar" /* ... */;