0
0
Swiftprogramming~20 mins

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

Choose your learning style9 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.