Swift while and repeat while Loop: In this tutorial, you will learn to create while and repeat…while loops in Swift programming.

In the previous tutorial, we learned about the Swift for-in loop to run a set of errands for a specific number of times. In this tutorial, you will learn to use while and repeat..while as an option of a for-in loop when the number of iteration is obscure.

A while loop executes a set of statements until a condition turns out to be false. These sorts of loops are best used when the number of iteration isn’t known before the first iteration starts. Swift provides two sorts of while loops:

The Repeat While loop is the same as while loop nut a difference that the body of repeat…while loop is executed once before the test expression is checked.


1. Swift while Loop

This loop assesses its condition toward the beginning of each pass through the loop.

The syntax of while loop is:

while (TestExpression) {
    // statements
}

How while loop works?

The TestExpression is a boolean expression.

If the TestExpression is evaluated to true,

  • statements inside the while loop are executed.
  • and the TestExpressionis evaluated again.

This interaction goes on until the TestExpression is assessed to false. In the event that the TestExpression assesses to false, while loop is ended.


Example 1: While Loop

var currentLevel:Int = 0, finalLevel:Int = 5
let gameCompleted = true
while (currentLevel <= finalLevel) {
    //play game
    if gameCompleted {
        print("You have passed level \(currentLevel)")
        currentLevel += 1
    }
}
print("outside of while loop")

At the point when you run the program, the output will be:

You have passed level 0
You have passed level 1
You have passed level 2
You have passed level 3
You have passed level 4
You have passed level 5
outside of while loop

In the above program, the variable currentLevel and finalLevel is introduced to 0 and constant gameCompleted is initialized to true.

In each emphasis of while loop, it checks the condition currentLevel <= finalLevel. In the event that the condition returns true, statements inside while loop is executed in any case the loop terminates.

Execution steps

IterationCondition (currentLevel <= finalLevel)Output
10 <= 5 (true)You have passed level 0
21 <= 5 (true)You have passed level 1
32 <= 5 (true)You have passed level 2
43 <= 5 (true)You have passed level 3
54 <= 5 (true)You have passed level 4
65 <= 5 (true)You have passed level 5
76 <= 5 (false)outside of while loop

2. Repeat while Loop

This loop assesses its condition toward the finish of each pass through the loop. The repeat…while loop is like while loop with one key difference. The body of repeat…while loop is executed once before the test expression is checked.

The syntax of repeat..while loop is:

repeat {
    // statements
    ...
} while (testExpression)

How repeat…while loop works?

The body of repeat…while loop is executed once (prior to checking the test expression). Only at that time, testExpression is checked.

On the off chance that testExpression is assessed to true, statements inside the body of the loop are executed, and testExpression is assessed once more. This interaction goes on until testExpression is assessed to false.

At the point when testExpression is false, the repeat..while loop ends.


Example 2: Repeat while Loop

var currentLevel:Int = 0, finalLevel:Int = 5
let gameCompleted = true
repeat {
    //play game
    if gameCompleted {
        print("You have passed level \(currentLevel)")
        currentLevel += 1
    }
} while (currentLevel <= finalLevel)
print("outside of repeat while loop")

At the point when you run the program, the output will be:

You have passed level 0
You have passed level 1
You have passed level 2
You have passed level 3
You have passed level 4
You have passed level 5
outside of repeat while loop

In the above example, for the first time the statements inside the loop executes. Furthermore, for next emphasis, it checks the condition currentLevel <= finalLevel.

In the event that the condition returns true, statements inside while loop is executed in any case the loop ends.

Execution steps

IterationCondition (currentLevel <= finalLevel)Output
10 <= 5 (true)You have passed level 0
21 <= 5 (true)You have passed level 1
32 <= 5 (true)You have passed level 2
43 <= 5 (true)You have passed level 3
54 <= 5 (true)You have passed level 4
65 <= 5 (true)You have passed level 5
76 <= 5 (false)outside of repeat while loop

Albeit both while and repeat while loop has the same execution steps, the condition currentLevel <= finalLevel on repeat while loop is executed only after executing the statements inside it.

However, in while, the condition is checked toward the start prior to executing the statements inside it.


3. Infinite while Loop

On the off chance that the test expression never assesses to false, the body of while and repeat..while loop is executed number of times.

Example 3: Infinite while Loop

while (true) {
   print("Hello, World!")
}
repeat {
   print("Hello, World!")

} while (true)

At the point when you run the program, the output will be:

Hello, World!
Hello, World!
.
.
.

At the point when you run the program, both loop executes print(“Hello, World!”) statements for an infinite number of times. Thus, you can see continuous output Hello, World! in the console.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.