Recall & Review
beginner
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: <br>for number in numbers where number % 2 == 0 {<br> print(number)<br>}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?✗ 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?✗ 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?✗ Incorrect
Multiple conditions can be combined with logical operators inside the
where clause.What happens if no elements satisfy the
where condition?✗ 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?✗ 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.