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

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.