Swift Switch Statement

The switch statement is used to perform different actions based on different conditions. It is similar to if-else statements, but provides a more concise syntax when handling multiple cases. In Swift, the switch statement is used with any data type and can have multiple cases.

The syntax for a basic switch statement in Swift is as follows:

1switch someValue { 2 case value1: 3 // do something 4 case value2: 5 // do something else 6 case value3: 7 // do something else again 8 default: 9 // do something by default 10}

In the above code, someValue is compared against value1, value2, and value3 in order. If someValue matches any of these values, the corresponding code block is executed. If no matches are found, the default code block is executed.

Here is an example that demonstrates a switch statement in Swift:

1let day = "Sunday" 2 3switch day { 4 case "Monday": 5 print("Today is Monday") 6 case "Tuesday": 7 print("Today is Tuesday") 8 case "Wednesday": 9 print("Today is Wednesday") 10 case "Thursday": 11 print("Today is Thursday") 12 case "Friday": 13 print("Today is Friday") 14 case "Saturday": 15 print("Today is Saturday") 16 case "Sunday": 17 print("Today is Sunday") 18 default: 19 print("Invalid day") 20}

In this example, we have a string variable day that contains the value "Sunday". The switch statement compares the value of day against each case. Since day matches the value "Sunday", the corresponding code block print("Today is Sunday") is executed.

Switch statement with range values

We can also use a range of values in a switch statement. For example:

1let score = 75 2 3switch score { 4 case 0..<60: 5 print("You failed the exam") 6 case 60..<70: 7 print("You passed the exam with a D") 8 case 70..<80: 9 print("You passed the exam with a C") 10 case 80..<90: 11 print("You passed the exam with a B") 12 case 90..<100: 13 print("You passed the exam with an A") 14 default: 15 print("Invalid score") 16}

In this example, we have an integer variable score that contains the value 75. The switch statement compares the value of score against each case. Since score falls in the range 70..<80, the corresponding code block print("You passed the exam with a C") is executed.

Switch statement with enum

Switch statements can also be used with enum types. For example:

1enum Direction { 2 case north 3 case south 4 case east 5 case west 6} 7 8let dir = Direction.north 9 10switch dir { 11 case .north: 12 print("You are heading north") 13 case .south: 14 print("You are heading south") 15 case .east: 16 print("You are heading east") 17 case .west: 18 print("You are heading west") 19}

In this example, we have an enum type Direction that contains four cases: north, south, east, and west. We have a variable dir of type Direction that contains the value .north. The switch statement compares the value of dir against each case. Since dir contains the value .north, the corresponding code block print("You are heading north") is executed.

Swift Nested Switch Statement

In Swift, a switch statement is used to evaluate a value against a number of cases and perform a block of code based on the matching case. A nested switch statement is a switch statement inside another switch statement. This allows for more complex and granular control flow in your code.

Nested Switch Syntax

The syntax of a nested switch statement in Swift is as follows:

1switch value1 { 2case value1_pattern1: 3 switch value2 { 4 case value2_pattern1: 5 // code to execute 6 case value2_pattern2: 7 // code to execute 8 default: 9 // code to execute 10 } 11case value1_pattern2: 12 // code to execute 13default: 14 // code to execute 15}

The inner switch statement can have its own cases, and the outer switch statement can handle the matching of values for the outer cases.

Example:

Let's take an example of a nested switch statement to determine the day of the week and whether it is a weekday or weekend:

1let dayOfWeek = "Saturday" 2let isWeekend: Bool 3 4switch dayOfWeek { 5case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday": 6 isWeekend = false 7 switch dayOfWeek { 8 case "Monday": 9 print("Today is Monday") 10 case "Tuesday": 11 print("Today is Tuesday") 12 case "Wednesday": 13 print("Today is Wednesday") 14 case "Thursday": 15 print("Today is Thursday") 16 case "Friday": 17 print("Today is Friday") 18 default: 19 print("Not a weekday") 20 } 21case "Saturday", "Sunday": 22 isWeekend = true 23 switch dayOfWeek { 24 case "Saturday": 25 print("Today is Saturday") 26 case "Sunday": 27 print("Today is Sunday") 28 default: 29 print("Not a weekend day") 30 } 31default: 32 print("Invalid day of week") 33} 34 35print("Is it a weekend? \(isWeekend)")

In this example, the outer switch statement checks whether the day of the week is a weekday or weekend. If it is a weekday, it sets the isWeekend variable to false and executes the inner switch statement to determine which weekday it is. If it is a weekend, it sets the isWeekend variable to true and executes the inner switch statement to determine which weekend day it is. Finally, it prints out whether it is a weekend or weekday based on the value of the isWeekend variable.

Output:

1Today is Saturday 2Is it a weekend? true