Bird
Raised Fist0
Swiftprogramming~20 mins

Repeat-while loop in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Repeat-while Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a simple repeat-while loop
What is the output of this Swift code using a repeat-while loop?
Swift
var count = 1
repeat {
    print(count)
    count += 1
} while count <= 3
A
1
2
3
B
1
2
3
4
C
2
3
4
D
0
1
2
3
Attempts:
2 left
💡 Hint
Remember that repeat-while runs the loop body first, then checks the condition.
❓ Predict Output
intermediate
2:00remaining
Repeat-while loop with condition false initially
What will this Swift code print?
Swift
var number = 5
repeat {
    print(number)
    number += 1
} while number < 5
AError: infinite loop
B5
C
5
6
7
8...
DNo output
Attempts:
2 left
💡 Hint
The repeat-while loop runs the body once before checking the condition.
🔧 Debug
advanced
2:00remaining
Identify the error in this repeat-while loop
What error does this Swift code produce?
Swift
var i = 0
repeat {
    print(i)
    i += 1
} while i = 5
ASyntaxError: Cannot assign to value in condition
BRuntimeError: Infinite loop
CNo error, prints 0 to 4
DTypeError: i is not a Boolean
Attempts:
2 left
💡 Hint
Check the condition syntax in the while statement.
🧠 Conceptual
advanced
2:00remaining
Repeat-while loop vs while loop behavior
Which statement correctly describes the difference between repeat-while and while loops in Swift?
Arepeat-while loops check the condition before executing the body
BBoth loops execute the body at least once regardless of condition
Crepeat-while executes the loop body at least once; while may not execute if condition is false initially
Dwhile executes the loop body at least once; repeat-while may not execute if condition is false initially
Attempts:
2 left
💡 Hint
Think about when the condition is checked in each loop type.
❓ Predict Output
expert
3:00remaining
Output of nested repeat-while loops
What is the output of this Swift code with nested repeat-while loops?
Swift
var x = 1
repeat {
    var y = 1
    repeat {
        print("(\(x),\(y))")
        y += 1
    } while y <= 2
    x += 1
} while x <= 2
A
(1,1)
(2,1)
(2,2)
B
(1,1)
(2,1)
(1,2)
(2,2)
C
(1,1)
(1,2)
(1,3)
(2,1)
(2,2)
D
(1,1)
(1,2)
(2,1)
(2,2)
Attempts:
2 left
💡 Hint
The inner loop runs fully for each iteration of the outer loop.

Practice

(1/5)
1. What is the main difference between a repeat-while loop and a while loop in Swift?
easy
A. A repeat-while loop runs only once and never repeats.
B. A repeat-while loop checks the condition before running the code block.
C. A repeat-while loop cannot be used with Boolean conditions.
D. A repeat-while loop runs the code block at least once before checking the condition.

Solution

  1. Step 1: Understand loop behavior

    A while loop checks the condition before running the code block, so it may not run at all if the condition is false initially.
  2. Step 2: Compare with repeat-while

    A repeat-while loop runs the code block first, then checks the condition to decide if it should repeat, guaranteeing at least one execution.
  3. Final Answer:

    A repeat-while loop runs the code block at least once before checking the condition. -> Option D
  4. Quick Check:

    Repeat-while runs first, then checks condition [OK]
Hint: Repeat-while always runs code once before checking condition [OK]
Common Mistakes:
  • Thinking repeat-while checks condition first
  • Confusing repeat-while with while loop
  • Believing repeat-while can't repeat
2. Which of the following is the correct syntax for a repeat-while loop in Swift?
easy
A. repeat (condition) { /* code */ }
B. repeat { /* code */ } while condition
C. while condition { /* code */ } repeat
D. do { /* code */ } while condition

Solution

  1. Step 1: Recall Swift syntax for repeat-while

    The correct syntax starts with the keyword repeat, followed by a code block in braces, then the while keyword with the condition.
  2. 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 uses do-while which is not valid Swift syntax.
  3. Final Answer:

    repeat { /* code */ } while condition -> Option B
  4. Quick Check:

    repeat { } while condition syntax [OK]
Hint: Remember: repeat { } while condition is the correct form [OK]
Common Mistakes:
  • Using do-while instead of repeat-while
  • Placing condition inside parentheses after repeat
  • Mixing while and repeat keywords
3. What will be the output of the following Swift code?
var count = 3
repeat {
    print(count)
    count -= 1
} while count > 0
medium
A. 3 2 1
B. 2 1 0
C. 3 2 1 0
D. 1 2 3

Solution

  1. Step 1: Trace the loop iterations

    Initial count is 3. The loop prints count, then decreases it by 1. It repeats while count > 0.
  2. 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.
  3. Final Answer:

    3 2 1 -> Option A
  4. Quick Check:

    Prints 3,2,1 then stops [OK]
Hint: Count down prints before condition check, stops at zero [OK]
Common Mistakes:
  • Including 0 in output
  • Printing after decrement instead of before
  • Confusing order of printed numbers
4. Identify the error in this Swift repeat-while loop code:
var x = 5
repeat {
    print(x)
    x += 1
} while x < 5
medium
A. No error; the loop runs exactly once.
B. The loop will never run because condition is false initially.
C. Syntax error: missing parentheses around condition.
D. The loop will run infinitely because x keeps increasing.

Solution

  1. 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.
  2. 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.
  3. 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.
  4. Final Answer:

    No error; the loop runs exactly once. -> Option A
  5. Quick Check:

    Loop runs once then stops [OK]
Hint: Check variable change and condition carefully to avoid infinite loops [OK]
Common Mistakes:
  • Assuming infinite loop without checking condition
  • Thinking condition needs parentheses
  • Believing loop won't run at all
5. You want to use a repeat-while loop to ask a user for a password until they enter "swift123". Which code snippet correctly implements this?
hard
A. repeat { print("Enter password:") input = readLine() ?? "" } while input == "swift123"
B. var input = "" while input != "swift123" { print("Enter password:") input = readLine() ?? "" }
C. var input = "" repeat { print("Enter password:") input = readLine() ?? "" } while input != "swift123"
D. var input = "" repeat { input = readLine() ?? "" } while input != "swift123" print("Enter password:")

Solution

  1. Step 1: Understand the requirement

    The loop must ask for password at least once and repeat until the user enters "swift123".
  2. 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.
  3. Final Answer:

    var input = "" repeat { print("Enter password:") input = readLine() ?? "" } while input != "swift123" -> Option C
  4. Quick Check:

    Repeat-while asks first, repeats until correct [OK]
Hint: Use repeat-while to ask first, then check condition [OK]
Common Mistakes:
  • Using while loop that may skip first prompt
  • Reversing condition logic
  • Prompting outside the loop
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard