November 2016
Intermediate to advanced
480 pages
14h 42m
English
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
Read now
Unlock full access