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 8-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:
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.
How to do it...
Let's use an if/else control flow statement to write a function to return the right pool ball type:
- Create an enum...