Swift switch Statement
Here, we'll go over the basics of using the switch statement in Swift.
Basics of the switch Statement in Swift
With if statements, conditions are evaluated and the code block runs when the condition is true.
With the switch statement, the code block for the case that matches the value of a variable is executed.
The basic syntax of the switch statement in Swift is as follows:
switch [variable] {
case [value1]:
[code block executed when variable == value1]
case [value2], [value3]:
[code block executed when variable == value2 or value3]
default:
[code block executed when none of the cases match]
}
You can have as many cases as you like.
The default case is not strictly required, but in Swift, the switch statement must ensure that one of the cases or the default block is executed. Otherwise, it results in an error.
It's also an error if a case or default does not have any executable code after it.
Unlike C or JavaScript, you don't need to explicitly write break for each case. The switch in Swift breaks by default.
Swift checks the cases from top to bottom, executes the first matching case, and exits the switch statement.
Let's try using a switch statement.
What will be the result of the following code?
let value = 4
switch value {
case 1:
print("value is 1.")
case 2, 4, 6, 8:
print("value is one of 2, 4, 6, or 8.")
default:
print("Other value.")
}
Since value is 4, the code block following case 2, 4, 6, 8 executes and prints "value is one of 2, 4, 6, or 8."
Using fallthrough in switch Cases
As mentioned earlier, Swift checks the cases from top to bottom and executes the first matching case before exiting the switch.
If you want the execution to continue into the next case instead of exiting immediately, you can add the fallthrough keyword at the end of the case block.
For example, if we change the first case in the earlier code to case 4, it looks like this:
let value = 4
switch value {
case 4:
print("value is 4.")
case 2, 4, 6, 8:
print("value is one of 2, 4, 6, or 8.")
default:
print("Other value.")
}
Since value is 4, only the first case block executes and the switch exits immediately.
This is valid, but you'll get a warning that the second case is redundant because the value has already been handled.
To make it intentionally fall through, you can write the code as follows:
let value = 4
switch value {
case 4:
print("value is 4.")
fallthrough
case 2, 4, 6, 8:
print("value is one of 2, 4, 6, or 8.")
default:
print("Other value.")
}
You'll still see a warning, but in this case both the first and the second case blocks are executed.
Using Ranges in switch Cases
In Swift, you can also specify ranges for case values in a switch statement.
To do this, use the range operators after case.
For example, you can write cases like this:
On line 6, the range 2..<10 matches values from 2 up to (but not including) 10, and on line 8, 10... matches values of 10 or greater.
let value = 4
switch value {
case 1:
print("value is 1.")
case 2..<10:
print("value is between 2 and 9.")
case 10...:
print("value is 10 or greater.")
default:
print("Other value.")
}
Since value is 4, the case 2..<10 block is executed.
That wraps up our explanation of the basics of the switch statement in Swift.