Do…While Loops in Swift (Do Once, Then Loop)

In Swift, there is no do…while loop that is common in other programming languages.

Instead, there is a repeat…while loop that does the exact same thing.

Here’s an example piece of code that uses this looping construct to run through some numbers:

var x = -10

repeat {
    x += 5
} while x > 0

print(x)

Output:

-5

The repeat block is always executed at least once no matter what.

Let’s take a detailed guide about repeat…while loops in Swift.

Repeat…While Loop in Swift

Here is an illustration of how the repeat…while loop works in Swift:

repeat while loop flowchart in swift

The repeat…while loop works such that it:

  1. Runs the code in the repeat block once.
  2. Checks the condition in the while statement.
  3. Runs the repeat block again if the condition holds.
  4. If the condition does not hold, the loop is exited.

Repeat…While vs While Loops in Swift

The difference between repeat…while and the while loops in Swift is:

  • The repeat…while loop always executes the statements at least once.
  • A while loop does not execute any statements if the condition is not satisfied.

Of course, another key difference between repeat…while and while loops is the syntax.

A regular while loop follows this syntax:

while condition {
    // statements
}

Whereas the repeat…while loop has this syntax:

repeat {
    // statements
} while condition

Next, let’s take a look at how to use the control flow statements in a repeat…while loop.

The break, continue, and return Statements in repeat…while Loop

Similar to how you can control the flow of a while loop, you can do it with repeat…while loops.

The control flow statements in Swift are:

  • continue
  • break
  • return

Let’s take a look at how each of these works with the repeat…while loop.

continue statement

The continue statement is used to skip the rest of a loop. Similar to how you can use the continue statement in a while/for loop, you can use it in a repeat…while loop.

For example:

let target = 5
var n: Int?

repeat {
    n = Int.random(in: 1...10)
    if n != target {
        print(n!)
        continue
    }
    print("The target was found")
} while n != target

Output:

7
2
9
4
9
1
The target was found

This piece of code skips printing the “The target was found” message until the target is actually found.

break statement

The break statement breaks the loop. This means it completely terminates the execution of the code.

You can use the break statement in a repeat…while loop the same way you would use it in a while loop.

To exit the repeat…while loop in Swift, use the break statement.

For example:

let target = 5
var n: Int?

repeat {
    n = Int.random(in: 1...10)
    print(n!)
    if n == target {
        print("Target found")
        break
    }
} while n != target

Output:

6
10
8
2
5
Target found

This code prints the “Target found” when the target number is equal to the randomly generated number. It then escapes the loop altogether using the break statement.

return statement

In Swift, the return statement exits a function.

If you have a loop inside a function, and you want to exit the function from the loop, use the return statement.

For example:

func find(_ target: Int) {
    var n: Int?

    repeat {
        n = Int.random(in: 1...10)
        print(n!)
        if n == target {
            print("Target found")
            return
        }
    } while n != target
}

find(5)

Output:

7
6
7
9
5
Target found

This function finds the target number using a repeat…while loop. It prints the “Target found” when the target number is equal to the randomly generated number. It then escapes the function using the return statement.

Why Use repeat…while Loop in Swift

Sometimes when looping, you want to repeat an action such that it happens at least once no matter what the condition.

In this case, you can use the repeat…while loop.

Conclusion

Today you learned what is the repeat…while loop in Swift.

To recap, some common programming languages have a do-while statement. In Swift, there is no do…while statement. Instead, there is a repeat…while which does the same thing. It runs a block of code before it checks the condition.

Thanks for reading. Happy coding!

Further Reading

50 Swift Interview Questions

Scroll to Top