Latest web development tutorials

Swift While loop

Swift cycle Swift cycle

Swift while loop from the beginning to calculate a single condition. If the condition is true, runs a series of statements is repeated until the condition becomes false.

grammar

Swift while loop syntax is as follows:

while condition
{
   statement(s)
}

Syntaxstatement (s) may be a statement or a block.condition can be an expression. If the condition is true, runs a series of statements is repeated until the condition becomes false.

The number 0, the string '0' and "", the empty list (), and undefined variables arefalse, then the others are true.true negated usingthe! number or not,it returns false inverted.

flow chart:

Examples

import Cocoa
 
var index = 10

while index < 20 
{
   print( "index 的值为 \(index)")
   index = index + 1
}

The above program execution output is:

index 的值为 10
index 的值为 11
index 的值为 12
index 的值为 13
index 的值为 14
index 的值为 15
index 的值为 16
index 的值为 17
index 的值为 18
index 的值为 19

Swift cycle Swift cycle