Chapter    22

Loops

When you want to repeat operations, you can use loops. Loops give you a neat way to do something a set number of times or to do operations until a certain condition (that you define) is met. Collection types such as arrays and dictionaries use loops for iteration.

for-condition-increment Loop

This for loop will be familiar to many programmers. The for-condition-increment loop gives you a loop that will execute a set number of times. You will have to use the for keyword along with an ending condition and an increment statement.

The loop shown in Listing 22-1 prints out 1 through 10 to the console as an example.

Listing 22-1. for-condition-increment Loop

for var i = 1; i <= 10; ++i {    println("i = \(i)")}

Let’s look at the ...

Get Swift Quick Syntax Reference 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.