What does the where clause do in a for-in loop in Swift?
It filters the items in the loop, so the loop only runs for elements that meet the condition specified in the where clause.
Click to reveal answer
beginner
How would you write a for-in loop that prints only even numbers from an array numbers?
Use a where clause to check if the number is even:
for number in numbers where number % 2 == 0 { print(number) }
Click to reveal answer
intermediate
Can you use multiple conditions in a where clause in Swift's for-in loop?
Yes, you can combine conditions using logical operators like && (and) or || (or) inside the where clause.
Click to reveal answer
beginner
What happens if no elements satisfy the where condition in a for-in loop?
The loop body does not run at all because no elements meet the condition to be processed.
Click to reveal answer
beginner
Why is using a where clause in a for-in loop helpful?
It makes the code cleaner and easier to read by filtering elements directly in the loop instead of using an if statement inside the loop.
Click to reveal answer
What does the where clause do in a Swift for-in loop?
ADeclares a new variable
BFilters elements to loop only over those that meet a condition
CEnds the loop early
DChanges the loop variable type
✗ Incorrect
The where clause filters elements so the loop runs only for those that satisfy the condition.
Which of these is a valid use of where in a for-in loop?
Afor item in items where item.isActive { print(item) }
Bfor item where item.isActive in items { print(item) }
Cwhere item.isActive for item in items { print(item) }
Dfor item in items if item.isActive { print(item) }
✗ Incorrect
The correct syntax places the where clause after the loop variable and before the loop body.
Can you use multiple conditions in a where clause?
AYes, using logical operators like && and ||
BNo, only one condition is allowed
CYes, but only with commas
DNo, you must use nested loops
✗ Incorrect
Multiple conditions can be combined with logical operators inside the where clause.
What happens if no elements satisfy the where condition?
AThe loop runs for all elements anyway
BThe loop runs once with a nil value
CThe loop throws an error
DThe loop body does not run
✗ Incorrect
If no elements meet the condition, the loop body is skipped entirely.
Why might you prefer using a where clause over an if statement inside the loop?
AIt runs faster always
BIt allows modifying the loop variable
CIt makes the code cleaner and easier to read
DIt automatically sorts the elements
✗ Incorrect
Using where filters elements upfront, making the loop simpler and clearer.
Explain how the where clause works in a Swift for-in loop and why it is useful.
Think about how you pick only certain items from a list to work with.
You got /3 concepts.
Write a Swift for-in loop using a where clause to print only odd numbers from an array.
Use modulo operator to check odd numbers.
You got /3 concepts.
Practice
(1/5)
1. What does the where clause do in a for-in loop in Swift?
easy
A. It repeats the loop twice for each item.
B. It changes the type of the loop variable.
C. It filters items so the loop runs only on those that meet the condition.
D. It stops the loop after the first item.
Solution
Step 1: Understand the role of where in a loop
The where clause adds a condition to filter items during iteration.
Step 2: Effect on loop execution
Only items that satisfy the where condition are processed in the loop.
Final Answer:
It filters items so the loop runs only on those that meet the condition. -> Option C
Quick Check:
where filters loop items [OK]
Hint: Remember: where filters items in the loop [OK]
Common Mistakes:
Thinking where changes variable type
Assuming it repeats the loop multiple times per item
Believing it stops the loop early
2. Which of the following is the correct syntax for using a where clause in a Swift for-in loop?
easy
A. for item where item.isActive in items { print(item) }
B. for item in items where item.isActive { print(item) }
C. for item in items if item.isActive { print(item) }
D. for item in items; where item.isActive { print(item) }
Solution
Step 1: Recall correct for-in where syntax
The correct syntax places where after the sequence: for variable in sequence where condition.
Step 2: Check each option
Only for item in items where item.isActive { print(item) } matches the correct syntax. The other options have misplaced or incorrect keywords: where before in, if instead of where, or a semicolon before where.
Final Answer:
for item in items where item.isActive { print(item) } -> Option B
Quick Check:
Correct where syntax [OK]
Hint: Put where right after in sequence [OK]
Common Mistakes:
Placing where before in
Using if instead of where
Adding semicolon before where
3. Given the array let numbers = [1, 2, 3, 4, 5], what will this code print?
for num in numbers where num % 2 == 0 {
print(num)
}
medium
A. 2 4
B. 1 2 3 4 5
C. 1 3 5
D. No output
Solution
Step 1: Understand the loop and condition
The loop iterates over numbers and prints only those where num % 2 == 0, meaning even numbers.
Step 2: Identify even numbers in the array
From [1, 2, 3, 4, 5], the even numbers are 2 and 4.
Final Answer:
2 4 -> Option A
Quick Check:
Print even numbers = 2 4 [OK]
Hint: Check condition filters even numbers only [OK]
Common Mistakes:
Printing odd numbers instead
Printing all numbers ignoring where
Expecting no output
4. What is wrong with this Swift code?
let names = ["Anna", "Bob", "Cara"]
for name where name.hasPrefix("A") in names {
print(name)
}
medium
A. The where clause is placed before in incorrectly.
B. The array names is not declared properly.
C. The hasPrefix method does not exist.
D. The loop variable name should be a constant.
Solution
Step 1: Check the for-in where syntax
The where clause must come after in sequence, not before.
Step 2: Identify the syntax error
The code places where before in names, which is invalid syntax.
Final Answer:
The where clause is placed before in incorrectly. -> Option A
Quick Check:
where must follow in [OK]
Hint: Always put where after in [OK]
Common Mistakes:
Placing where before in
Assuming hasPrefix is invalid
Thinking loop variable must be var
5. You have a list of user records as dictionaries: