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.
Practice
repeat-while loop and a while loop in Swift?Solution
Step 1: Understand loop behavior
Awhileloop checks the condition before running the code block, so it may not run at all if the condition is false initially.Step 2: Compare with
Arepeat-whilerepeat-whileloop runs the code block first, then checks the condition to decide if it should repeat, guaranteeing at least one execution.Final Answer:
Arepeat-whileloop runs the code block at least once before checking the condition. -> Option DQuick Check:
Repeat-while runs first, then checks condition [OK]
- Thinking repeat-while checks condition first
- Confusing repeat-while with while loop
- Believing repeat-while can't repeat
repeat-while loop in Swift?Solution
Step 1: Recall Swift syntax for repeat-while
The correct syntax starts with the keywordrepeat, followed by a code block in braces, then thewhilekeyword with the condition.Step 2: Check each option
repeat { /* code */ } while condition matches the correct syntax exactly. repeat (condition) { /* code */ } incorrectly places the condition in parentheses after repeat. while condition { /* code */ } repeat mixes while and repeat incorrectly. do { /* code */ } while condition usesdo-whilewhich is not valid Swift syntax.Final Answer:
repeat { /* code */ } while condition -> Option BQuick Check:
repeat { } while condition syntax [OK]
- Using do-while instead of repeat-while
- Placing condition inside parentheses after repeat
- Mixing while and repeat keywords
var count = 3
repeat {
print(count)
count -= 1
} while count > 0Solution
Step 1: Trace the loop iterations
Initial count is 3. The loop prints count, then decreases it by 1. It repeats while count > 0.Step 2: List printed values
First iteration: print 3, count becomes 2.
Second iteration: print 2, count becomes 1.
Third iteration: print 1, count becomes 0.
Loop stops because count is not > 0.Final Answer:
3 2 1 -> Option AQuick Check:
Prints 3,2,1 then stops [OK]
- Including 0 in output
- Printing after decrement instead of before
- Confusing order of printed numbers
repeat-while loop code:var x = 5
repeat {
print(x)
x += 1
} while x < 5Solution
Step 1: Analyze loop condition and variable change
Initial x is 5. The loop runs once before checking condition. Inside loop, x increases by 1 each time.Step 2: Check condition after first iteration
After first iteration, x becomes 6. Condition is x < 5, which is false, so loop should stop. But since x increases, if condition was true, it would never stop. Here, it stops after one iteration.Step 3: Re-examine the condition logic
Actually, since x starts at 5, condition x < 5 is false, so loop runs once then stops. No infinite loop.Final Answer:
No error; the loop runs exactly once. -> Option AQuick Check:
Loop runs once then stops [OK]
- Assuming infinite loop without checking condition
- Thinking condition needs parentheses
- Believing loop won't run at all
repeat-while loop to ask a user for a password until they enter "swift123". Which code snippet correctly implements this?Solution
Step 1: Understand the requirement
The loop must ask for password at least once and repeat until the user enters "swift123".Step 2: Check each option
var input = "" repeat { print("Enter password:") input = readLine() ?? "" } while input != "swift123" uses repeat-while, asks inside loop, and repeats while input is not "swift123". This matches the requirement.
var input = "" while input != "swift123" { print("Enter password:") input = readLine() ?? "" } uses while loop, which may skip asking if input is already "swift123".
repeat { print("Enter password:") input = readLine() ?? "" } while input == "swift123" repeats while input equals "swift123", which is opposite of requirement.
var input = "" repeat { input = readLine() ?? "" } while input != "swift123" print("Enter password:") prints prompt after loop ends, so user is not prompted before input.Final Answer:
var input = "" repeat { print("Enter password:") input = readLine() ?? "" } while input != "swift123" -> Option CQuick Check:
Repeat-while asks first, repeats until correct [OK]
- Using while loop that may skip first prompt
- Reversing condition logic
- Prompting outside the loop
Ready to prove it?
Pass the test and this pattern moves unknown
