0
0
Swiftprogramming~10 mins

While loop in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - While loop
Initialize variable
Check condition
|Yes
Execute loop body
Update variable
Back to Check
Exit loop
Back to Check
The while loop starts by checking a condition. If true, it runs the loop body, updates variables, then checks again. It stops when the condition is false.
Execution Sample
Swift
var count = 1
while count <= 3 {
    print(count)
    count += 1
}
This code prints numbers 1 to 3 using a while loop that runs while count is less or equal to 3.
Execution Table
StepcountCondition (count <= 3)ActionOutput
11trueprint 1, count = 21
22trueprint 2, count = 32
33trueprint 3, count = 43
44falseexit loop
💡 count becomes 4, condition 4 <= 3 is false, loop stops
Variable Tracker
VariableStartAfter 1After 2After 3Final
count12344
Key Moments - 2 Insights
Why does the loop stop when count is 4 even though the loop body has a print statement?
Because the condition count <= 3 is checked before the loop body runs (see step 4 in execution_table). When count is 4, the condition is false, so the loop exits without printing.
What happens if we forget to update count inside the loop?
The condition will always be true (count stays 1), causing an infinite loop. The execution_table shows count increasing each step to avoid this.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of count at step 3?
A2
B3
C4
D1
💡 Hint
Check the 'count' column in execution_table row for step 3.
At which step does the condition become false and the loop stops?
AStep 4
BStep 2
CStep 3
DStep 1
💡 Hint
Look at the 'Condition' column in execution_table to find when it is false.
If we change the initial count to 4, what will happen to the loop?
AIt will run infinitely
BIt will print 4 once and stop
CIt will not print anything and stop immediately
DIt will print numbers from 4 to 3
💡 Hint
Refer to variable_tracker and condition check in execution_table; condition is false if count starts at 4.
Concept Snapshot
Swift while loop syntax:
while condition {
    // code to repeat
}

- Checks condition before each loop
- Runs loop body only if condition is true
- Stops when condition is false
- Remember to update variables inside loop to avoid infinite loops
Full Transcript
This visual execution shows how a Swift while loop works. We start with a variable count set to 1. The loop checks if count is less or equal to 3. If yes, it prints count and increases it by 1. This repeats until count becomes 4, when the condition fails and the loop stops. The execution table tracks each step, showing count values, condition results, actions, and outputs. The variable tracker shows how count changes after each iteration. Key moments explain why the loop stops before printing when condition is false and the importance of updating count to avoid infinite loops. The quiz tests understanding of count values at steps, when the loop stops, and effects of changing initial count.