Swift repeat...while loops
Unlike for and while loops, which check the condition before the loop body runs, a Swift repeat...while loop checks whether the condition is met at the end of the loop execution.
Syntax
The syntax of a Swift repeat...while loop is as follows:
repeat
{
statement(s);
}while( condition );
Note that the condition expression appears at the bottom of the loop, so the statement(s) in the loop execute at least once before the condition is tested.
If the condition is true, control flow jumps back to repeat above and re-executes the statement(s) in the loop. This process repeats until the given condition becomes false.
The number 0, the strings '0' and "", an empty list(), and undefined variables are all false , and everything else is true. Negating true uses ! the sign or not, returning false after negation.
Flow diagram:
Example
import Cocoa
var index = 15
repeat{
print( "index 的值为 \(index)")
index = index + 1
}while index < 20
The output of the above program is:
index 的值为 15
index 的值为 16
index 的值为 17
index 的值为 18
index 的值为 19
Swift Loops