Making decisions with if/else
The if/else
statement is a cornerstone of almost every programming language. It enables code to be executed conditionally, based on the outcome of a Boolean statement. In this recipe, we will see how if/else
can be used, including some ways that are unique to Swift.
Getting ready
If you have ever played pool, you’ll know that the aim of the game (when playing standard eight-ball pool) is to pot all the balls of one type and then to pot the black ball. When using American pool balls, they are numbered 1–15 and have a different pattern, depending on their type. Balls 1–7 have a solid color, balls 9–15 are white with a colored stripe around them, and ball 8 is black:
Figure 3.1 – American pool balls
In this recipe, we will write a function that will take the number on a pool ball and return the type of ball it is.
Let’s create a new playground for this recipe.