Bird
0
0

Identify the error in this Swift code:

medium📝 Debug Q6 of 15
Swift - Loops
Identify the error in this Swift code:
let values = [1, 2, 3, 4]
for val where val > 2 in values {
    print(val)
}
AThe <code>where</code> clause is placed before <code>in</code>
BThe loop variable <code>val</code> is not declared
CThe array <code>values</code> is not initialized
DThe print statement is missing parentheses
Step-by-Step Solution
Solution:
  1. Step 1: Check for-in loop syntax

    The where clause must come after in values, not before.
  2. Step 2: Identify the misplaced where

    Here, where val > 2 is incorrectly before in values.
  3. Final Answer:

    The where clause is placed before in -> Option A
  4. Quick Check:

    where goes after in [OK]
Quick Trick: Always put where after in collection [OK]
Common Mistakes:
  • Placing where before in
  • Forgetting to declare loop variable
  • Misusing print syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes