O'Reilly logo

Swift Programming: The Big Nerd Ranch Guide by John Gallagher, Matthew Mathias

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

Control Transfer Statements, Redux

Let’s revisit control transfer statements in the context of loops. Recall from Chapter 5 (where you used fallthrough and break) that control transfer statements change the typical order of execution. In the context of a loop, you can control whether execution iterates to the top of the loop or leaves the loop altogether.

Let’s elaborate on the space shooter game to see how this works. You are going to use the continue control transfer statement to stop the loop where it is and begin again from the top.

Listing 6.6 Using continue

...
var shields = 5
var blastersOverheating = false
var blasterFireCount = 0
while shields > 0 {

    if blastersOverheating {
        print("Blasters are overheated! Cooldown ...

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