Control Transfer Statements in Loops

Let’s revisit control transfer statements in the context of loops. Recall from Chapter 5, Switch (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.

Example 6.6. Using continue
...
var shields = 5                                            5
var blastersOverheating = false                            false
var blasterFireCount = 0                                   0
while shields > 0 {

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

Get Swift Programming: The Big Nerd Ranch Guide, 3rd Edition 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.