0
0
Swiftprogramming~10 mins

For-in with where clause in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - For-in with where clause
Start loop over collection
Check where condition
Execute loop body
Next element or end loop
The loop goes through each item, checks the condition after 'where', runs the body only if true, otherwise skips to next.
Execution Sample
Swift
let numbers = [1, 2, 3, 4, 5]
for num in numbers where num % 2 == 0 {
    print(num)
}
Loops over numbers, printing only even ones (where condition filters items).
Execution Table
IterationnumCondition (num % 2 == 0)ActionOutput
11FalseSkip iteration
22TrueExecute loop body2
33FalseSkip iteration
44TrueExecute loop body4
55FalseSkip iteration
💡 All elements checked; loop ends after last element.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
numnil123455
Key Moments - 2 Insights
Why does the loop skip some numbers instead of printing them?
Because the where clause filters items. Only numbers where num % 2 == 0 (even numbers) run the loop body, as shown in execution_table rows 1,3,5 where condition is False and action is skip.
Does the loop stop early when the condition is false?
No, the loop continues checking all elements. It only skips executing the body for elements that don't meet the condition, as seen in execution_table where all iterations run but some skip the body.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'num' at iteration 4?
A3
B2
C4
D5
💡 Hint
Check the 'num' column in execution_table row for iteration 4.
At which iteration does the condition 'num % 2 == 0' become true for the first time?
AIteration 2
BIteration 1
CIteration 3
DIteration 5
💡 Hint
Look at the 'Condition' column in execution_table to find first True.
If the where clause was removed, what would change in the execution table?
AThe loop would skip all iterations.
BAll iterations would execute the loop body and print all numbers.
COnly odd numbers would be printed.
DThe loop would run zero times.
💡 Hint
Without where clause, no filtering happens, so all elements run the loop body.
Concept Snapshot
for item in collection where condition {
    // code runs only if condition true
}

- Loops over collection
- 'where' filters items
- Body runs only if condition true
- Skips items failing condition
- Loop continues through all elements
Full Transcript
This visual execution shows a Swift for-in loop with a where clause. The loop goes through each number in the array. For each number, it checks if the number is even (num % 2 == 0). If true, it runs the loop body and prints the number. If false, it skips printing and moves to the next number. The variable 'num' changes each iteration to the current number. The loop does not stop early; it checks all numbers. This filtering behavior is controlled by the where clause. The execution table tracks each step, showing which numbers print and which skip. This helps beginners see how the where clause filters items in a loop.