O'Reilly logo

Swift Quick Syntax Reference by Matthew Campbell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required