5 Switch

In an earlier chapter, you saw one sort of conditional statement: if/else. Along the way, we discussed that if/else can be somewhat inadequate in scenarios that have more than a few conditions. This chapter looks at the switch statement. Unlike if/else, switch is ideal for handling multiple conditions. As you will see, Swift’s switch statement is an incredibly flexible and powerful feature of the language.

What Is a Switch?

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

Get Swift Programming: The Big Nerd Ranch Guide 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.