Bird
0
0

What is the purpose of using a where clause in a for-in loop in Swift?

easy📝 Conceptual Q1 of 15
Swift - Loops
What is the purpose of using a where clause in a for-in loop in Swift?
ATo filter elements during iteration based on a condition
BTo declare the loop variable type explicitly
CTo create a nested loop inside the <code>for-in</code> loop
DTo break the loop when a condition is met
Step-by-Step Solution
Solution:
  1. Step 1: Understand the role of where in loops

    The where clause filters elements so only those meeting the condition run inside the loop.
  2. Step 2: Compare with other options

    Declaring types, nesting loops, or breaking loops are not functions of where in for-in.
  3. Final Answer:

    To filter elements during iteration based on a condition -> Option A
  4. Quick Check:

    Purpose of where clause = Filter elements [OK]
Quick Trick: Use where to pick only items you want in the loop [OK]
Common Mistakes:
  • Thinking where declares variable types
  • Confusing where with loop break
  • Assuming where creates nested loops

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes