Bird
Raised Fist0
Swiftprogramming~10 mins

While loop in Swift - Step-by-Step Execution

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

Practice

(1/5)
1. What does a while loop do in Swift?
easy
A. Repeats code while a condition is true
B. Runs code only once
C. Runs code a fixed number of times
D. Stops the program immediately

Solution

  1. Step 1: Understand the purpose of a while loop

    A while loop runs repeatedly as long as its condition stays true.
  2. Step 2: Compare options with the definition

    Only Repeats code while a condition is true correctly describes repeating code while a condition is true.
  3. Final Answer:

    Repeats code while a condition is true -> Option A
  4. Quick Check:

    While loop = repeat while true condition [OK]
Hint: While loops run as long as condition is true [OK]
Common Mistakes:
  • Thinking while loops run a fixed number of times
  • Confusing while with if statements
  • Assuming while loops run only once
2. Which of the following is the correct syntax for a while loop in Swift?
easy
A. while condition: { /* code */ }
B. while (condition) do { /* code */ }
C. while { condition } /* code */
D. while condition { /* code */ }

Solution

  1. Step 1: Recall Swift while loop syntax

    Swift uses: while condition { code } without parentheses or extra keywords.
  2. Step 2: Check each option

    while condition { /* code */ } matches Swift syntax exactly; others have extra keywords or wrong punctuation.
  3. Final Answer:

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

    Swift while syntax = while condition { } [OK]
Hint: Swift while loops use no parentheses around condition [OK]
Common Mistakes:
  • Adding parentheses around the condition
  • Using 'do' keyword incorrectly
  • Using colon instead of braces
3. What will be the output of this Swift code?
var count = 3
while count > 0 {
    print(count)
    count -= 1
}
medium
A. 1 2 3
B. 3 2 1
C. 3 2 1 0
D. 0 1 2 3

Solution

  1. Step 1: Analyze the loop condition and variable

    Variable count starts at 3 and loop runs while count > 0.
  2. Step 2: Trace each iteration

    Prints 3, then count becomes 2; prints 2, count 1; prints 1, count 0; loop stops.
  3. Final Answer:

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

    Loop prints count down from 3 to 1 [OK]
Hint: Count down from 3 to 1, stops before 0 [OK]
Common Mistakes:
  • Including 0 in output
  • Printing in ascending order
  • Not decreasing count inside loop
4. Identify the error in this Swift code:
var i = 1
while i < 5 {
    print(i)
}
medium
A. Variable i should be a constant
B. Incorrect while syntax
C. Missing increment of i inside the loop
D. print statement is invalid

Solution

  1. Step 1: Check loop structure and variable changes

    The loop condition depends on i, but i never changes inside the loop.
  2. Step 2: Understand effect of missing increment

    Without increasing i, the condition i < 5 stays true forever, causing an infinite loop.
  3. Final Answer:

    Missing increment of i inside the loop -> Option C
  4. Quick Check:

    Loop variable must change to avoid infinite loop [OK]
Hint: Always update loop variable inside while loop [OK]
Common Mistakes:
  • Forgetting to increment or change loop variable
  • Thinking print causes error
  • Assuming syntax is wrong
5. How can you use a while loop to sum all even numbers from 2 to 10 inclusive in Swift?
hard
A. Use a variable starting at 2, add it to sum, increment by 2 until 10
B. Start at 1, add all numbers, then multiply by 2
C. Use a while loop with condition i <= 10, increment i by 2, add i
D. Use a while loop starting at 10, increment 2 each time, add to sum

Solution

  1. Step 1: Plan loop to add even numbers from 2 to 10

    Start with i = 2, add i to sum, then increase i by 2 to get next even number.
  2. Step 2: Check options for correct approach

    Use a variable starting at 2, add it to sum, increment by 2 until 10 correctly describes starting at 2, adding to sum, incrementing by 2 until 10.
  3. Final Answer:

    Use a variable starting at 2, add it to sum, increment by 2 until 10 -> Option A
  4. Quick Check:

    Sum evens by stepping 2 from 2 to 10 [OK]
Hint: Increment by 2 to sum even numbers quickly [OK]
Common Mistakes:
  • Incrementing by 2 without starting at 2
  • Starting from 10 and incrementing
  • Multiplying sum incorrectly
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