What if you could ask a question once and let the computer handle all the repeating for you?
Why Repeat-while loop in Swift? - Purpose & Use Cases
Imagine you want to keep asking a friend if they want to play a game until they say yes. You have to ask at least once, then keep asking again and again if they say no.
If you try to do this by writing the question many times manually, it becomes boring and you might forget to ask again or stop too early. It's slow and easy to make mistakes.
The repeat-while loop lets you ask the question once, then automatically keep asking as long as the answer is no. It saves time and avoids errors by repeating the steps for you.
print("Do you want to play? (yes/no)") var answer = readLine() ?? "" if answer != "yes" { print("Do you want to play? (yes/no)") answer = readLine() ?? "" // ... repeated many times }
var answer = "" repeat { print("Do you want to play? (yes/no)") answer = readLine() ?? "" } while answer != "yes"
You can run a set of actions at least once and then repeat them easily until a condition is met, making your code cleaner and smarter.
When filling out a form, you want to check the input at least once and keep asking the user to correct it until it's valid. Repeat-while loop handles this smoothly.
Repeat-while loop runs code at least once before checking a condition.
It helps avoid repeating code manually and reduces mistakes.
Great for tasks that need to happen first, then repeat based on a condition.