Latest web development tutorials

Swift repeat ... while loop

Swift cycle Swift cycle

Swift repeat ... while loop like for and while loop before the loop started to determine the conditional statement, but at the end of the cycle is determined whether the execution condition.

grammar

Swift repeat ... while loop syntax is as follows:

repeat
{
   statement(s);
}while( condition );

Please note that the conditional expressions appear at the end of the loop, so the loop statement (s) will be executed at least once before the condition is tested.

If the condition is true, the flow of control jumps back to the top of the repeat, then repeat loop statement (s). This process is repeated until the given 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 = 15

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

The above program execution output is:

index 的值为 15
index 的值为 16
index 的值为 17
index 的值为 18
index 的值为 19

Swift cycle Swift cycle