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 Development, Coding Ninjas Studio Problems, Coding Ninjas Studio Interview Bundle, Coding Ninjas Studio Interview Experiences, Coding Ninjas Courses, Coding Ninjas Studio Contests, and Coding Ninjas Studio Test Series. Do upvote our blog in helping other ninjas grow.
Delighted Programming!