Loops

After the branching structures if and switch, the most common structures you'll use in your programming are looping structures, which cause your program flow to execute the same code iteratively.

The looping structures you'll learn in this section are the following:

  • for…in, which executes the same code a predetermined number of times
  • while and repeat…while, which executes code until a true condition becomes false

As with the switch control structure, there are many features and flexible options provided by these structures that make Swift more expressive and powerful than many other programming languages.

The for…in Statement

The following diagram illustrates how the for...in statement works:

Most programming languages have a for statement used ...

Get Beginning Swift 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.