Swift switch Statement


A switch statement in Swift is a type of flow control that completes the execution of a program immediately after the first match of the case or condition instead of checking to the bottom of subsequent cases.

You can think of a switch statement as the advanced form of the if statement. The switch statement is also known as the switch/case statement.

The control flow program of comes out from the switch case statement as soon as the conditions match.

Syntax of switch case statement in Swift

The switch keyword is used to create a switch statement and the case keywords are used for a switch statement to check if the value matches.

Below is the very generic syntax of the switch statement in Swift:

switch (expression)  {
  case value1:
    // statements 
    // Optional break statement

  case value2:
    // statements 
    // Optional break statement

  case value3:
    // statements 
    // Optional break statement

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

In the above syntax, the switch statement is used to evaluate an expression inside parentheses (). We are checking for the value of the expression using the case statement.

if between value1, value2, or value3 any one of these values matches the expression, then the particular statement for the case will be executed and break the switch statement. If there still remains case statement at the bottom, it will be ignored completely after the matched value.

The optional default statement will be executed if no value will match the expression.

Example of a switch case statement

Now let’s see an example now:

let number = 2

switch number {
case 0:
    print("This is zero")

case 1:
    print("This is one")

case 2:
    print("This is two")

default:
    print("Nothing matches")
}

Output

This is two

In the above example, we are checking the number variable. Using each of the case statements, we are comparing the value of the number variable.

As the value of the number is 2, so the statement print("This is two") will run.

Switch Case Statement Flowchart

Below is the diagram for the Flowchart of the Switch Case Statement:

Flowchart of Switch case Statement
Flowchart of Switch case Statement

Using range in the switch statement

You can use range in the switch statement. Below is an example where we are using the range in the switch statement:

let age = 27

switch age {
  case 0...14:
    print("Child")

  case 15...28:
    print("Adult")

  case 29...45:
    print("Middle-aged")

  default:
    print("Old-aged")
}

Output

Adult

In the above example, we are using numeric ranges for case statements that are case 0…14, case 15…28 and case 29…45.

Here the age is 27, it falls within the range 15…28. So the print("Adult") statement has been executed.

Using fallthrough in the switch statement

Inside the case statement, we can use the fallthrough keyword. If we use this keyword, then the next case statement will also be executed even though it doesn’t match the expression.

Below is an example of using the fallthrough keyword inside the case statement:

// program to find the day of the week 

let dayNumber = 5

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

Output

Its Thursday
Its Friday

From the output of the above program, you can see that not only the code block run for the matched case but also the next code block also run though it doesn’t match the expression. That’s the working of the fallthrough in a switch statement.