repeat-while Loops
Swift also supports a type of while loop called the repeat-while loop.
The repeat-while loop is called a do-while loop in other languages.
The difference between while and repeat-while loops is when they evaluate their condition.
The while loop evaluates its condition before stepping into the loop.
This means that the while loop may not ever execute, because its condition could be false when it is first evaluated.
The repeat-while loop, on the other hand, executes its loop at least once, and then evaluates its condition.
The syntax for the repeat-while loop demonstrates this difference.
repeat {
// Fire blasters!
print("Fire blasters!")
} while shields > 0
In this repeat-while version of the space shooter game, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access