Chapter 5. Switch

In Chapter 3, Conditionals, you saw one sort of conditional statement: if/else. Along the way, we mentioned that if/else is not great for scenarios that have more than a few conditions. This chapter looks at the switch statement, which is ideal for handling multiple conditions. As you will see, Swift’s switch statement is a flexible and powerful feature of the language.

if/else statements execute code based on whether the condition under consideration evaluates to true. switch statements consider a particular value and attempt to match it against a number of expressions, called cases. If there is a match, the switch executes the code associated with that case. In this chapter, you will explore the use of switch statements ...

Get Swift Programming: The Big Nerd Ranch Guide, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.