Javatpoint Logo
Javatpoint Logo

Swift While and Repeat While Loop

While 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:

  1. While loop
  2. Repeat While loop

Swift While Loop

The Swift while loop evaluates its condition at the start of each pass.

Syntax:

Here, TestExpression is a Boolean expression. If it is true,

  • Statements inside the while loop are executed.
  • And, the TestExpressionis evaluated again.

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

Swift while Statement

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.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA