Chapter 5. Flow Control and More
This chapter presents the miscellaneous remaining aspects of the Swift language. I’ll start by describing the syntax of Swift’s flow control constructs for branching, looping, and jumping. Then I’ll talk about how to override operators and how to create your own operators. The chapter ends with a survey of Swift’s privacy and introspection features and some specialized modes of reference type memory management.
A computer program has a path of execution through its code statements. Normally, this path follows a simple rule: execute each statement in succession. But there is another possibility. Flow control can be used to make the path of execution skip some code statements, or repeat some code statements. Flow control is what makes a computer program “intelligent,” and not merely a simple fixed sequence of steps. By testing the truth value of a condition — an expression that evaluates to a Bool and is thus
false — the program decides at that moment how to proceed. Flow control based on testing a condition may be divided into two general types:
- The code is divided into alternative chunks, like roads that diverge in a wood, and the program is presented with a choice of possible ways to go: the truth of a condition is used to determine which chunk will actually be executed.
- A chunk of code is marked off for possible repetition: the truth of a condition is used to determine whether the chunk should be executed, ...