On a daily basis, we’re faced with making decisions based on certain conditions; if the weather is beautiful, we’ll go for a walk, or if it’s rainy, we’ll stay in and code!
In Swift, the ability to determine an outcome based on a given condition also exists. It’s known as a conditional.
Conditionals are powerful programming tools that introduce flexibility and complex behavior to a program by allowing it to react to change. Their power comes from being the root of all decision making within an application.
Your computer, for example, contains authentication processes whose logic is handled by conditionals. If you are logged out, access to certain pages on your device may be restricted, or if your are logged in, access will be granted.
In this lesson, we’ll set up our own rules and conditions for programs to follow using Swift’s conditional statements and operators.
Take a look at our Swifty Donut 🍩 factory on the right. Notice how conditionals are responsible for all the processes within this factory. If a donut should be chocolate flavored, it lands on one conveyor belt, if it’s strawberry, it lands on another and so on!
What’s another conditional you can think of used in this factory?