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

while Loops

A while loop executes the code inside its body so long as a condition is true. You can write while loops to do many of the same things you have seen in for loops above. For example, a while loop that replicates the for loop in Listing 6.1 can be expressed like so:

Listing 6.5 A while loop

...
var i = 1
while i < 6 {
    myFirstInt += 1
    print(myFirstInt)
    i += 1
}

Figure 6.5 shows the flow of execution in this code.

Figure 6.5 while loop diagram

A flowchart depicts the “while” loop diagram.

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