0
0
Swiftprogramming~5 mins

Repeat-while loop in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a repeat-while loop in Swift?
A repeat-while loop runs the code inside its block at least once, then keeps repeating as long as the condition is true. It's like doing something first, then checking if you should do it again.
Click to reveal answer
beginner
How does a repeat-while loop differ from a while loop?
A repeat-while loop runs the code block first, then checks the condition. A while loop checks the condition first before running the code. So, repeat-while always runs at least once.
Click to reveal answer
beginner
Write a simple Swift repeat-while loop that counts from 1 to 3.
var count = 1 repeat { print(count) count += 1 } while count <= 3 This prints 1, 2, and 3 each on a new line.
Click to reveal answer
intermediate
Why might you choose a repeat-while loop over a while loop?
Use repeat-while when you want the code to run at least once no matter what, like asking a user for input and then checking if it’s valid.
Click to reveal answer
beginner
What happens if the condition in a repeat-while loop is false right after the first run?
The loop stops after the first run because the condition is false. But the code inside still runs once before stopping.
Click to reveal answer
What is guaranteed about the code inside a repeat-while loop?
AIt runs at least once
BIt might not run at all
CIt runs only if the condition is true initially
DIt runs twice before checking the condition
Which keyword starts a repeat-while loop in Swift?
Awhile
Bdo
Crepeat
Dloop
When does the condition get checked in a repeat-while loop?
ABefore the loop body runs
BAfter the loop body runs
CAt the same time as the loop body runs
DOnly once at the start
What will this code print? var x = 5 repeat { print(x) x -= 1 } while x > 3
ANothing
B5 only
C5, 4, 3
D5 and 4
If the condition is false at the start, how many times does a repeat-while loop run?
AOnce
BZero times
CTwice
DInfinite times
Explain how a repeat-while loop works and when you would use it.
Think about doing something first, then checking if you should do it again.
You got /3 concepts.
    Write a Swift repeat-while loop that prints numbers from 1 to 5.
    Start with a number 1, print it, add 1, and repeat while number is less or equal to 5.
    You got /4 concepts.