O'Reilly logo

Swift Programming: The Big Nerd Ranch Guide by John Gallagher, Matthew Mathias

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

5 Switch

In an earlier chapter, you saw one sort of conditional statement: if/else. Along the way, we mentioned 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required