Swift While Loop
Swift while loop starts by evaluating a single condition. If the condition is true, a series of statements will be run repeatedly until the condition becomes false.
Syntax
The syntax format of the Swift while loop is as follows:
while condition
{
statement(s)
}
In the syntax,statement(s)can be a statement or a statement block.conditioncan be an expression. If the condition is true, a series of statements will be run repeatedly until the condition becomes false.
The number 0, the string '0' and "", the empty list(), and undefined variables are allfalse, while others are alltrue. To negate true, use the!symbol ornot, which returns false after negation.
Flowchart:
Example
import Cocoa
var index = 10
while index < 20
{
print( "index 的值为 \(index)")
index = index + 1
}
The output of the above program execution is:
index 的值为 10 index 的值为 11 index 的值为 12 index 的值为 13 index 的值为 14 index 的值为 15 index 的值为 16 index 的值为 17 index 的值为 18 index 的值为 19Other Extensions
Swift Loops