Swift While and Repeat While LoopWhile and Repeat while loops are used as an alternative of for-in loops when the number of iteration is not known. The while loop executes a set of statements until a false condition occur. This loop is generally used when you don't know about the number of iterations. There are two types of loops in Swift: Swift While LoopThe Swift while loop evaluates its condition at the start of each pass. Syntax:Here, TestExpression is a Boolean expression. If it is true,
This process goes on until the TestExpression evaluates false. When the TestExpression gets the false condition, the while loop is terminated. Flow chart of While loop Example:Output: You have successfully completed level 0 You have successfully completed level 1 You have successfully completed level 2 You have successfully completed level 3 You have successfully completed level 4 You have successfully completed level 5 You have successfully completed level 6 Terminated! You are out of the game In the above program, the while loop is executed until the condition is evaluated to false and as soon as it gets the false condition, it terminates.
Next TopicSwift Repeat While loop
|