Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. SwiftBeginner - The while and repeat-while Loops

The while and repeat-while Loops - Images: Japanese

Hey there, everyone!

Next up, let's take a look at the 'while statement' and the 'repeat-while statement'. We'll start with the 'while statement'.

The while statement is a loop construct that works similarly to the for-in statement. While for-in is far more commonly used for looping, the while statement tends to come into play when the number of iterations isn't known in advance.

Let's start with a quick look at an example.

var n = 0

while n < 3 { // Loops as long as variable 'n' is less than '3'.
    print(n)
    n = n + 1
}

And here's the output.

0
1
2

The loop runs until 'n' reaches '2'. That's the 'while statement' in action.

Now let's go over the syntax. The basics are super straightforward. You write 'while', followed by a 'condition expression' right next to it, and then put the code you want to loop inside '{}'. That's it.

In pseudocode, it looks like this.

while condition {
    // code here...
}

As long as the condition evaluates to true, the loop keeps running.

One thing to note: the condition must evaluate to a Boolean value (true or false). Other languages often let you get away with other values, but Swift is strict about this — so watch out.

while 1 { // Error — '1' is not a valid condition expression.

}

That's really all there is to the syntax. Pretty simple, right?

Now, here's a super important heads-up.

If you've been paying attention, you may have noticed that the 'while statement' has no built-in mechanism for updating the condition.

That makes while quick and easy to write, but it also means you can accidentally create an infinite loop without much effort.

Here's what that looks like. The following is an infinite loop built with a 'while statement'.

// Do NOT run this.
while true {
    // code here...
}

All you need is 'true' as the condition — and boom, infinite loop. So be really careful about what you put in that condition expression.

A good habit to drill into yourself: whenever you write a 'while statement', always include an update to the condition somewhere inside the loop. In the first example, that was the 'n = n + 1' part. Don't forget it.

Modern software and devices are pretty resilient, so an infinite loop from a 'while statement' is unlikely to cause anything catastrophic... but it does put unnecessary strain on the computer, so do be careful.

Alright, next up is the 'repeat-while statement'. Take a look at the example below.

while false {
    print("Executed!")
}

The condition here is literally 'false', which means the loop body will never run — not even once.

But sometimes you might be in the mood to run the code at least once, no matter what.

That's where the 'repeat-while statement' comes in. Here's what it looks like.

repeat {
    print("Executed!")
} while false

Running this gives you:

Executed!

As you can see, the 'repeat-while statement' is a 'while statement' that always runs at least once.

The syntax: write 'repeat', followed by '{}' with the code you want to execute inside. Then write 'while' after the closing brace, followed by a space and your condition expression.

In pseudocode:

repeat {
    // code here...
} while condition

Compared to the regular 'while statement', the condition is written at the end. Everything else is the same.

That's really the only difference, so you should be good to go.

One more general tip: when you're building a loop and either 'while' or 'for-in' would work, it's generally safer to go with 'for-in'.

The reason is that 'while' makes it easy to accidentally create an infinite loop with a small typo — and that's a slightly higher risk. This applies to other languages too, so it's worth keeping in the back of your mind.

Swift's 'repeat-while statement' is essentially the same as what's called a 'do-while statement' in other languages.

Here's the slightly confusing part: older versions of Swift actually used 'do-while' instead of 'repeat-while'.

// This is how it was written in older versions of Swift.
do {
    print("Executed!")
} while false

In current Swift, do has been replaced with repeat — so heads up on that.

The only change is the keyword from do to repeat; everything else is the same. Still, it's easy to mix up, so keep it in mind.

And that covers the 'while statement' and 'repeat-while statement'! In the next article, we'll go over 'break' and 'continue'. See you there!

Until next time!

This article was written by Sakurama.

Author's beloved small mammal

桜舞 春人 Sakurama Haruto

A Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him.

If you find any errors or copyright issues, please .