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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access