Chapter 8

Controlling the Flow

In This Chapter

arrow Redirecting the flow of Swift’s code with flow controls

arrow Using for and while loops

arrow Working with conditions

Code is executed sequentially — line after line of code. That sequential flow is interrupted in two ways:

  • Through the use of flow controls — code structures that modify the sequential flow of code. Usually the term flow controls refers to conditional statements and various types of loops. Occasionally they explicitly change the flow by forcing the app to jump out of the current function either to a specific command or, more frequently, to whatever it was that called the function. All of these force the “next” line of code to be something other than the next line of code in the source file.
  • By functions and closures — sections of code implemented in response to certain conditions. Those conditions can be events, such as the completion of an asynchronous process, or they can be references within the code that fire off synchronous or asynchronous processes. All of these disrupt the next-line structure to start processing in response to an event.

These concepts are implemented in different ways in different languages. This chapter ...

Get Swift For Dummies now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.