Programming is all about making decisions. The purpose of most code involves taking information, inspecting it, making decisions, and producing an output. So far, we have seen a lot of ways to represent information, but in this chapter, we will explore how to make decisions based on that information using a number of Swift's control flow statements. We will find out how they differ and the situations where each is appropriate.
In this chapter, we will cover the following recipes:
- Making decisions with if/else
- Handling all the cases with switch
- Looping with for loops
- Looping with while loops
- Handling errors with try, throw, do, and catch
- Checking upfront with guard
- Doing it later with defer
- Bailing out with fatalError and precondition