Zach Olivare - 2015 Mar 25
Why do most languages still require breaks in a switch statement? Is Swift the beginning of the end for these break statements?
switch statements. Specifically, I think it is fantastic that Swift switch statements do not fall through the bottom of each case and into the next one by default. Instead, the entire switch statement finishes its execution as soon as the first matching case is completed, without requiring an explicit break statement.switch statement design, I began thinking about a question that I've had since I was first introduced to switch statements in SE 1011; why do we require the programmer to insert an implicit break statement inside of every case?break because that's the way it was done in C and the designers of new languages always want their syntax to be easily understood by programmers who are comfortable with this C-like syntax.switch statement is basically an abstraction of a branch table, which has implicit fall-through.switch statement is interesting because it requires that explicit flow control (break, goto, return or throw) occur at the end of a case and will throw a compile time error if that flow control is omitted. Fall-through can be achieved only by using an explicit goto at the end of a case. MSDN says the reason for this is that although "implicit fall-through behavior is often used to reduce the amount of code needed", "as code moves from the initial development phase into a maintenance phase, [implicit fall-through] can lead to subtle errors that are very hard to debug".break (or other control flow) in switch statements, I looked into several up and coming languages to see how they implemented switch statements.break in switch statements is not universal, it appears as though it will still be around for years to come.