O'Reilly logo

Mastering Swift 4 - Fourth Edition by Jon Hoffman

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

The while loop

The while loop executes a block of code until a condition is met. Swift provides two forms of the while loop; these are the while and repeat-while loops. In Swift 2.0, Apple replaced the do-while loop with the repeat-while loop. The repeat-while loop functions in the same way as the do-while loop did. Swift uses the do statement for error handling.

We use while loops when the number of iterations to perform is not known and usually dependent on some business logic. A while loop is used when you want to run a loop zero or more times, while a repeat-while loop is used when you want to run the loop one or more times.

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