Swift Tutorials: switch with Multiple Cases 🎯

beginner
6 min

Swift Tutorials: switch with Multiple Cases 🎯

Welcome to our comprehensive guide on the switch statement in Swift! In this lesson, we'll dive deep into using the switch statement with multiple cases, a powerful tool for handling complex decision-making scenarios in your code. 💡 Pro Tip: The switch statement is an excellent alternative to long if-else chains, making your code cleaner, more readable, and easier to maintain.

What is switch? 📝

The switch statement in Swift is a control structure that allows you to compare a value against multiple cases. It's a more efficient and expressive way to execute different blocks of code based on the value of a variable or expression.

Syntax and Basic Usage 📝

Here's the basic syntax of the switch statement in Swift:

swift
switch expression { case constant1: // code to execute if expression equals constant1 case constant2: // code to execute if expression equals constant2 // ... default: // code to execute if expression does not match any case }

In the above syntax, expression is the value you want to compare against the cases. Each case represents a possible value the expression can evaluate to. The default case is an optional final case that executes if the expression does not match any of the other cases.

Using switch with Multiple Cases 📝

Now, let's focus on using the switch statement with multiple cases. This allows you to handle multiple possibilities for the expression in a concise and readable way.

swift
let fruit = "apple" switch fruit { case "apple": print("The fruit is an apple.") case "banana": print("The fruit is a banana.") case "orange": print("The fruit is an orange.") case "grape": print("The fruit is a grape.") default: print("The fruit is not recognized.") }

In the example above, we have a variable fruit that stores a fruit name. We then use a switch statement to check the value of fruit against multiple cases. If the value of fruit matches a case, the corresponding code block is executed.

Advanced Examples 💡

Switching on an enumeration 📝

Enums (Enumerations) are powerful data types in Swift that let you define a set of related values. Using enums with switch statements can help make your code even more readable and expressive.

swift
enum Fruit { case apple, banana, orange, grape } let fruit: Fruit = .apple switch fruit { case .apple: print("The fruit is an apple.") case .banana: print("The fruit is a banana.") case .orange: print("The fruit is an orange.") case .grape: print("The fruit is a grape.") }

In this example, we've defined an enumeration called Fruit with four cases. Then, we assign one of these cases to the variable fruit. The switch statement uses the fruit variable and checks it against each case in the enumeration.

Switching on a range 📝

Swift's switch statement also allows you to compare expressions against a range of values. This can be useful when working with collections or looping through arrays and dictionaries.

swift
let number = 5 switch number { case 1...3: print("The number is between 1 and 3.") case 4...6: print("The number is between 4 and 6.") default: print("The number is outside the range.") }

In this example, we're checking if the variable number falls within a range. The ... operator defines a range, and the switch statement checks if the value of number falls within that range.

Quiz 🎯

Quick Quiz
Question 1 of 1

What is the purpose of the `switch` statement in Swift?

Quick Quiz
Question 1 of 1

Which of the following is not a correct way to use the `switch` statement in Swift?