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    26

Labeled Statements

Sometimes your programs can become complicated when you start to nest control structures such as loops, if statements, and switch statements. With Swift, you can label control statements and then use control transfer statements (Chapter 25). You can use the break, continue, and fallthrough control statements with labeled statements.

For instance, let’s assume you have two nested loops to print integers from 1 to 3, as shown in Listing 26-1.

Listing 26-1. Nested Loop

for x in 1...3 {    for y in 1...3 {        println("x = \(x), y = \(y)")    }}

The code from Listing 26-1 will print out this:

x = 1, y = 1x = 1, y = 2x = 1, y = 3x = 2, y = 1x = 2, y = 2x = 2, y = 3x = 3, y = 1x = 3, y = 2x = 3, y = 3

If you wanted ...

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