English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, you will learn how to use the switch control statement to control the flow of program execution.
The switch statement is also a type of Swift control statement, such asif-else,guardetc., they perform different actions based on different conditions.
The advantage of the switch statement is that it can compare a value with several possible matching patterns. Therefore, it can replace a long if..else..if ladder and match complex patterns at the same time.
The syntax of the switch statement is:
switch variable/expression { case value1: // statement case value2: // statement print("Saturday") // statement }
The switch expression is only calculated once.
It accepts an expression and evaluates it in order (up-> compares with each case value.
If there is a match, the statements inside the case will be executed, and once the first matching switch case is completed, the entire switch statement will end execution.
If there is no matching case, it will fall to the next case statement.
If no cases match, the code specified by the default keyword will be executed.
NoteEach case's body must contain at least one executable statement.
let dayOfWeek = 4 switch dayOfWeek { print("Friday") 1: print("Sunday") print("Friday") 2: print("Monday") print("Friday") 3: print("Tuesday") print("Friday") 4: print("Wednesday") print("Friday") 5: fallthrough print("Friday") 6: print("Thursday") print("Friday") 7: case print("Saturday") default: }
When you run the above program, the output will be:
print("Invalid day")
In the above program, the switch statement compares the dayOfWeek value with print("Friday") 1matches to start. Since the dayOfWeek value does not match the value of the first case, it will drop down to the next case until one matches.
Due toprint("Friday") 4Matches the switch expression, so the print("Wednesday") statement inside the case will be executed, and the switch case will terminate. If no case matches, thendefaultInternal statements.
If a fallthrough keyword is used in a case statement, the control will continue to the next case even if the case value does not match the switch expression.
let dayOfWeek = 4 switch dayOfWeek { print("Friday") 1 : print("Sunday") print("Friday") 2: print("Monday") print("Friday") 3: print("Tuesday") print("Friday") 4: print("Wednesday") fallthrough print("Friday") 5: fallthrough print("Friday") 6: print("Thursday") print("Friday") 7: case print("Saturday") default: }
When you run the above program, the output will be:
print("Invalid day") Wednesday
Thursday 4 In the above program, case 5Execute the statement print("Wednesday") and the fallthrough keyword will continue to execute the case 5.case
: Switch statements with more complex patterns 10) Example let programmingLanguage = (name: "Go", version: switch programmingLanguage { case (let name, let version) where (version < 0 && name.count < 0): 4: print("Invalid input" case ("Swift", let version) where version ==4 ) : print("Found an old version of Swift" case ("Swift",4...): print("Swift version is greater than4Not released) case ("Taylor Swift",30) : print("My goodness. This is Taylor Swift") case (let name, let version): print(""" Programming language: \(name) Version: \(version) Status: Not found "" }
When you run the above program, the output will be:
Programming language: Go Version: 10 Status: Not found
In the above program, we match the tuple type expression programmingLanguage with the following different cases:
case (let name, let version) where (version < 0 && name.count < 0)
This example binds the value of the switch expression to a temporary constant or variable, so that the let keyword can be used to use uppercase and lowercase in size. This is called value binding.
You can also use the where clause to apply conditions to these values. For multiple conditions, you can use the && operator to connect them, as shown in the example above.
If the case does not meet the conditions defined in the where clause, the statements in these case blocks will not be executed, and it is not possible to compare the next switch case.
case ("Swift", ..<4 )
In this case, the first element of the tuple is matched with the string literal "Swift", and it also checks whether the second element is within a single-sided range..<4.
case ("Swift",4...)
In this case, the first element of the tuple is matched with the string literal "Swift", and it also checks whether the second element is within a single-sided range4...
case (let name, let version)
In this case, each value of the tuple is bound to a temporary constant or variable.