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.
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.
Let's write a function that will take the number on the pool ball and return to us the type of ball it is.
How to do it...
Let's take a look at these steps to write this function, and then we will review how it works:
- First, let's create
enum
to describe the possible types:
enum PoolBallType { case solid case stripe case black }
- Next, we'll create the method that will take an
Int
and return...