Cookbook – SWIFT for Beginners – Chapter 12: Switch

Free eBooks for Beginners

Swift is a powerful and intuitive programming language for iOS, macOS, watchOS, and tvOS. It is designed to be easy to read and write, and it is a great language for beginner programmers to start learning. One of the most important concepts in Swift programming is the switch statement. In this article, we will take a closer look at what switch statements are, how they work, and why they are useful.

What is a Switch Statement?

A switch statement is a control flow statement that allows you to execute different code depending on the value of a variable. For example, you might have a switch statement that checks the value of a variable, and based on that value, it runs different code. This is similar to an if-else statement, but it is more concise and easier to read.

Why are Switch Statements Useful?

Switch statements are useful because they allow you to handle multiple cases in a concise and readable way. For example, if you have a variable that can have different values, you can use a switch statement to handle each case in a separate block of code. This makes your code easier to maintain and less prone to bugs.

How to Use a Switch Statement in Swift

To use a switch statement in Swift, you write the keyword switch followed by the variable you want to check. Then, you write a series of cases, each with its own code to run. For example:

let number = 5 

switch number { 
  case 1: print("The number is 1") 
  case 2: print("The number is 2") 
  case 5: print("The number is 5") 
  default: print("The number is not 1, 2, or 5") 
}

In this example, the switch statement checks the value of the number variable. If the value is 1, it will print “The number is 1”. If the value is 2, it will print “The number is 2”. If the value is 5, it will print “The number is 5”. If the value is not any of these, it will run the code in the default case.

Using Multiple Cases with the Same Code

Sometimes, you might want to run the same code for multiple cases. You can do this by listing multiple cases separated by commas, like this:

let number = 2 

switch number { 
   case 1, 3, 5: 
        print("The number is odd") 
   case 2, 4, 6: 
        print("The number is even") 
   default: 
        print("The number is not 1, 2, 3, 4, 5, or 6") 
  }

In this example, the switch statement checks if the value of the number variable is odd or even, and it runs the appropriate code.

Using a Range of Values

You can also use a range of values in a switch statement. For example:

let number = 10 

switch number { 
   case 0..<5: 
     print("The number is less than 5") 
   case 5..<10: 
     print("The number is between 5 and 10") 
   case 10..<15: 
     print("The number is between 10 and 15") 
   default: 
     print("The number is not in any of the defined ranges") 
}

In this example, the switch statement checks the value of the number variable, and it runs the appropriate code based on which range the number falls into.

Cookbook – SWIFT for Beginners – Chapter 12: Switch

Loader Loading...
EAD Logo Taking too long?

Reload Reload document
| Open Open in new tab

Download PDF [346.54 KB]

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.