3

Currently I have this:

let somePoint = (1, 0) switch somePoint { case (0,0): print("origin") // does not print fallthrough case (_, 0): print("y-axis") // prints y-axis. this makes sense fallthrough case(0, _): print("x-axis") // prints x-axis (because of fallthrough? this should not print) fallthrough case(-2...2, -2...2): print("in 5x5 box about the origin") // this prints and makes sense default: print("somewhere else") // does not print } 

My goal with this switch statement is to have each case print if it is true and NOT to have only the first case that is matched print. I thought I could do this with the fallthrough statement. However, this has made me question how it works. Why does fallthrough automatically print the next case in line even if the case does not match? How could I go about making this switch statement work the way I want it to?

2 Answers 2

7

Fallthrough falls through to the next case, not to the next matching case. The concept is inherited from C switch statements, where each case may be thought of as a goto destination label, and the switch statement brings execution to the first matching one.

In C, the switch statement only dictates where execution starts inside the block. For added convenience, you may use the break statement to skip the rest of the switch body, but nothing forces you to; and if you don't, execution continues normally, like the cases weren't there. For instance:

switch (countdown) { case 3: puts("3..."); case 2: puts("2..."); case 1: puts("1..."); case 0: puts("0!"); } 

With no break anywhere, if countdown is 3, then you get the whole thing (even though countdown is obviously 3, and not 2, 1 or 0).

When execution goes from a case to another instead of exiting the switch scope (for instance, with a break statement), you get "fall through" (which is what the Swift fallthrough keyword does).

This is relevant in C as you are allowed to use arbitrarily complex structures inside switch statements, overlapping cases if you want. Here is a legal C program:

switch (x) { case 0: if (y == 3) { case 1: puts("hello"); } else { puts("world"); } case 2: puts("!"); } 

This kind of use is however extremely uncommon and often hard to follow (quick! can the else branch be executed if x == 1?). I haven't tested, but I would be very surprised if you could do something similar with Swift.

In general, in C, fall through is considered poor style since it is often hard to tell if the fall through is voluntary or due to a missing break statement. Swift solves this with the fallthrough statement, which makes it explicit that you want execution to continue to the next case inside the switch statement rather than exit the switch scope.

In your case, you cannot use fallthrough to get what you want, because fallthrough is only useful when the execution sequence you need is linear. You need to skip over invalid blocks of code, so you need to use an if-else sequence instead of a switch statement.

Sign up to request clarification or add additional context in comments.

2 Comments

in C# it's solved by using goto case X, easy to read without the need of a new keyword
@LưuVĩnhPhúc, that's possible because of the limited nature of C# (and C/C++/etc) cases (relative to Swift) and not really what the OP wants. They want to go to another case and start searching for another match. C# goto case doesn't re-evaluate because the selection criteria are not dynamic. In other words, you already have to know where you're going.
4

If you're looking to fall through to other matching cases rather than the next (as fallthrough does), switch is not the right tool for the job.

Instead, you should use a series of if statements, something like:

Bool gotOne = false; if (somePoint.0 == 0 && somepoint.1 == 0) { print("origin") gotOne = true; } if (somepoint.1 == 0) { print("y-axis") gotOne = true; } if (somepoint.0 == 0) { print("x-axis") gotOne = true; } : if (! gotOne) { print("somewhere else") } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.