0
0
Swiftprogramming~5 mins

While loop in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a while loop in Swift?
A while loop repeats a block of code as long as a condition is true. It checks the condition before running the code each time.
Click to reveal answer
intermediate
How does a while loop differ from a repeat-while loop in Swift?
A while loop checks the condition before running the code, so it might not run at all. A repeat-while loop runs the code first, then checks the condition, so it runs at least once.
Click to reveal answer
beginner
Write a simple Swift while loop that counts from 1 to 5.
var count = 1 while count <= 5 { print(count) count += 1 }
Click to reveal answer
beginner
What happens if the condition in a while loop never becomes false?
The loop runs forever, causing an infinite loop. This can freeze or crash your program.
Click to reveal answer
beginner
Why is it important to update variables inside a while loop?
Updating variables changes the condition so the loop can eventually stop. Without updates, the loop might run forever.
Click to reveal answer
What does a while loop do in Swift?
AStops the program immediately
BRuns code only once
CRuns code after checking the condition once
DRepeats code while a condition is true
When does a while loop check its condition?
AOnly once at the start
BAfter running the loop code
CBefore running the loop code
DIt never checks
What will happen if the condition in a while loop is always true?
AThe loop runs forever (infinite loop)
BThe loop runs once
CThe loop never runs
DThe program crashes immediately
Which of these is a correct way to stop a while loop?
AChange the condition variable inside the loop
BDo nothing
CUse a for loop instead
DWrite code outside the loop
What is the output of this code? var i = 3 while i > 0 { print(i) i -= 1 }
A1 2 3
B3 2 1
C0 1 2
DNo output
Explain how a while loop works in Swift and why updating the condition variable inside the loop is important.
Think about what happens if the condition never changes.
You got /3 concepts.
    Write a simple Swift while loop that prints numbers from 1 to 5 and explain each step.
    Start with a variable set to 1 and increase it each time.
    You got /4 concepts.