English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Swift while and repeat...while loops

In this article, you will learn how to create while and repeat... while loops in Swift programming.

In the previous article, we learnedFor-In loopTo run a set of tasks a certain number of times. In this article, you will learn to use while and repeat..while as for loops when the number of iterations is unknown.-As an alternative to the in loop.

The while loop executes a set of statements until the condition becomes false. It is best to use this type of loop when the number of iterations is unknown at the beginning of the first iteration. Swift provides two types of while loops:

1. Swift While loop

This loop evaluates its condition at the beginning of each pass through the loop.

The syntax of the while loop is:

while (TestExpression) {
    // Statements
}

How does the while loop work?

TestExpression is a boolean expression.

If the TestExpression is calculated as true,

  • The statements within the while loop will be executed.

  • And the TestExpression is recalculated again.

Continue this process until the TestExpression is calculated as false. If the TestExpression is calculated as false, the while loop terminates.

While loop flowchart

Example1While loop

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

When you run this program, the output is:

You have passed level 0
You have passed the level 1
You have passed the level 2
You have passed the level 3
You have passed the level 4
You have passed the level 5
Outside the while loop

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

In each iteration of the while loop, it checks the judgment condition currentLevel <= finalLevel. If the condition returns true, the statements within the while loop are executed, otherwise the loop terminates.

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

2Repeat while loop

This loop evaluates its condition at the end of each iteration. The repeat ... while loop is similar to the while loop, but there is a key difference. Before checking the test expression, the body of the repeat ... while loop is executed once.

The syntax of the repeat..while loop is: }}

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

How does the repeat...while loop work?

The body of the repeat...while loop is executed once (before checking the test expression). Only then is the testExpression checked.

If the testExpression is calculated as true, the statements inside the loop are executed, and then the testExpression is calculated again. This continues until the testExpression is calculated as false.

The repeat...while loop terminates when the testExpression is false.

Flowchart of the repeat ... while loop

Example2:repeat ... while loop

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

When you run this program, the output is:

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

In the above example, the statements inside the loop are executed for the first time. For the next iteration, it checks the condition currentLevel <= finalLevel.

If the condition returns true, execute the statements inside the while loop; otherwise, the loop terminates.

Execution steps
IterationCondition (currentLevel <= finalLevel)Output
10 <= 5 (true)You have passed level 0
21 <= 5 (true)You have passed the level 1
32 <= 5 (true)You have passed the level 2
43 <= 5 (true)You have passed the level 3
54 <= 5 (true)You have passed the level 4
65 <= 5 (true)You have passed the level 5
76 <= 5 (false)

Outside of the repeat while loop

Although the repeat and repeat while loops have the same execution steps, the condition currentLevel <= finalLevel is only evaluated after the statements inside the repeat... while loop in the repeat... while loop.

However, in the while, the condition currentLevel <= finalLevel is checked before the execution condition is executed.

3Infinite while loop

If the test expression is never calculated as false, the body of the while and repeat..while loops is executed infinitely.

Example3Infinite while loop

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

 

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

When you run this program, the output is:

Hello, World!
Hello, World!
.
.
.

When you run the program, both loops will execute the statement print("Hello, World!") infinitely. Therefore, you can see the continuous output in the console"Hello, World!".