Swift switch Statement

The switch statement allows us to execute a block of code among many alternatives.

The syntax of the switch statement in Swift is:

switch (expression)  {
  case value1:
    // statements 

  case value2:
    // statements 

  ...
  ...
        
  default:
    // statements
}

The switch statement evaluates an expression inside parentheses ().

  1. If the result of the expression is equal to value1, statements of the case value1: are executed.
  2. If the result of the expression is equal to value2, statements of the case value2: are executed.
  3. If there is no match, statements of the default case are executed.

Note: We can do the same thing with the if...else...if ladder. However, the syntax of the switch statement is cleaner and much easier to read and write.


Flowchart of Switch Statement

Switch statement allows us to execute certain code blocks out of many alternatives.
Flowchart of Switch Statement

Example 1: Simple program using Switch Statement

// program to find the day of the week 

let dayOfWeek = 4

switch dayOfWeek {
  case 1:
    print("Sunday")
	    
  case 2:
    print("Monday")
	    
  case 3:
    print("Tuesday")
	    
  case 4:
    print("Wednesday")
	    
  case 5:
    print("Thursday")
	    
  case 6:
    print("Friday")
	    
  case 7:
    print("Saturday")
	    
  default:
    print("Invalid day")
}

Output

Wednesday

In the above example, we have assigned 4 to the dayOfWeek variable. Now, the variable is compared with the value of each case statement.

Since the value matches with case 4, the statement print("Wednesday") inside the case is executed, and the program is terminated.


Switch Statement with fallthrough

If we use the fallthrough keyword inside the case statement, the control proceeds to the next case even if the case value does not match with the switch expression. For example,

// program to find the day of the week 

let dayOfWeek = 4

switch dayOfWeek {
  case 1:
    print("Sunday")
	    
  case 2:
    print("Monday")
	    
  case 3:
    print("Tuesday")
	    
  case 4:
    print("Wednesday")
    fallthrough
	    
  case 5:
    print("Thursday")
	    
  case 6:
    print("Friday")
	    
  case 7:
    print("Saturday")
	    
  default:
    print("Invalid day")
}

Output

Wednesday
Thursday

In the above example, since the value matches with case 4, the statement print("Wednesday") inside the case is executed

We have also used the keyword fallthrough. So, the statement print("Thursday") inside case 5 is executed even if the case doesn't match with the case statement.


Example 2: Switch Statement with Range

let ageGroup = 33

switch ageGroup {
  case 0...16:
    print("Child")

  case 17...30:
    print("Young Adults")

  case 31...45:
    print("Middle-aged Adults")

  default:
    print("Old-aged Adults")
}

Output

Middle-aged Adults

In the above example, instead of a single value, we have used numeric ranges for each case: case 0...16, case 17...30, and case 31...45.

Here, the value of ageGroup is 33 that falls under the range 32...45. Hence, the statement print("Middle-aged Adults") is executed.


Tuple in Switch Statement

In Swift, we can also use tuples in switch statements. For example,

let info = ("Dwight", 38)

// match complete tuple values
switch info {
  case ("Dwight", 38): 
    print("Dwight is 38 years old")

  case ("Micheal", 46): 
    print("Micheal is 46 years old")

  default:
    print("Not known")
}

Output

Dwight is 38 years old

In the above example, we have created a tuple named info with values: "Dwight" and 38.

Here, instead of a single value, each case statement also includes tuples: case ("Dwight", 38) and case ("Micheal", 46).

Now, the value of info is compared with each case statement. Since the value matches with case ("Dwight", 38), the statement print("Dwight is 38 years old") is executed.

To learn more about tuples, visit Swift Tuple.