Table of contents
1.
Introduction
2.
Syntax
3.
Flowchart of Switch Statement
4.
Examples using Switch Statements in Swift
4.1.
Basic program using Switch Statement
4.1.1.
Output:
4.2.
Switch Statement with Fallthrough
4.2.1.
Output:
4.3.
Switch Statement with Range
4.3.1.
Output:
5.
Tuple in Switch Statement
5.1.
Output:
6.
Frequently Asked Questions
6.1.
Is it necessary to break Swift with switch statements?
6.2.
How do switch statements work in Swift?
6.3.
What are some of the most prevalent switch statement issues?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

Swift Switch Statements

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will learn how to manage the flow of your program's execution with switch control statements.

In Swift, a switch statement completes its execution as soon as the first matching case is completed, rather than falling through the bottom of subsequent cases as in C and C++. The switch statement allows us to choose between several options when running a block of code.

We'll go over the entire architecture and features of Swift Switch Statements in the following chapters.

Syntax

switch (expression)  {
  case val1:
    // statements

  case val2:
    // statements

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

The switch statement examines a parenthesized expression ().

 

  • If the outcome of the expression equals val1, the case val1: statements are executed.
  • If the outcome of the expression equals val2, the case val2: statements are executed.
  • If there is no match, the standard case statements are used.


Note that the if-else-if ladder can be used in the same way. The syntax of the switch statement, on the other hand, is significantly cleaner and easier to read and write. 

We must utilize the break statement to exit a case statement; otherwise, execution control will fall via the succeeding case statements below to the matching one.

Flowchart of Switch Statement

Examples using Switch Statements in Swift

Basic program using Switch Statement

//to find the day of the week
let dayOfWeek = 6

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")

// any other number as dayOfWeek
  default:
    print("Invalid day")
}

Output:

Friday

The dayOfWeek variable has been set to 6 in the example above. The variable is now compared to each case statement's value.The statement print("Friday") inside the case is executed because the value matches case 6, and the program is terminated.

Switch Statement with Fallthrough

Even if the case value does not correspond to the switch expression, the control will continue to the next case if the fallthrough keyword is used inside the case statement. For instance,

Code:

//to find the day of the week
let dayOfWeek = 6
 
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")
    fallthrough
       
  case 7:
    print("Saturday")
 
// any other number as dayOfWeek
  default:
    print("Invalid day")
}

Output:

Friday
Saturday

Because the value corresponds to case 6, the statement print("Friday") is executed within the case.

The term "fallthrough" was also used. As a result, even if the case does not match the statement, the statement print("Saturday") inside case 7 is executed.

Switch Statement with Range

Code:

//to find the ageGroup with age
let ageGroup = 20

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:

Young Adults

Instead of a single value, we used numeric ranges in the above example: case 0...16, 17...30, and 31...45.

The value of ageGroup, in this case, is 20, which falls within the range 17-30. As a result, the print("Young Adults") statement is executed.

Tuple in Switch Statement

Tuples can also be used in switch statements in Swift. For instance,

Code:

let info = ("Devoo", 21)

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

  case ("Deepu", 20):
    print("Deepu is 20 years old")

  default:
    print("Not known")
}
 

Output:

Devoo is 21 years old

In the example above, we created a tuple named info with the values "Devoo" and 21.

Each case statement now has two tuples instead of a single value: case ("Devoo," 21) and case ("Devoo," 21"). ("Deepu", 20).

The information value is now compared to each case statement. The statement print("Devoo is 21 years old") is executed because the value matches the case ("Devoo",  21).

Frequently Asked Questions

Is it necessary to break Swift with switch statements?

Although the break statement isn't required in Swift, you can use it to match and ignore a specific case or to exit a matched case before it completes its execution. See Break in a Switch Statement for more information. Each case must have at least one executable statement in its body.

How do switch statements work in Swift?

In Swift, the switch statement allows you to inspect a value and compare it to a set of cases. It's beneficial for making quick decisions based on a single variable with multiple possible values. Using the switch statement often results in shorter, easier-to-read code.

What are some of the most prevalent switch statement issues?

The most severe issue with switch statements is that they can cause code smells. Switch overuse could indicate that you're not using polymorphism properly in your code.

Conclusion

In this blog, we have extensively discussed the Swift Switch Statements. We hope that this article has provided you with additional information about the switch statements in swift. And to learn in-depth about Swift functions, check out the course on our Swift on the Coding Ninjas website. And also, check out the awesome content on the Coding Ninjas Website, Android DevelopmentCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview Experiences, Coding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test Series. Do upvote our blog in helping other ninjas grow.

Delighted Programming!

Live masterclass