Bird
0
0

Which of the following is the correct syntax for a for-in loop iterating over an array named fruits in Swift?

easy📝 Syntax Q12 of 15
Swift - Loops
Which of the following is the correct syntax for a for-in loop iterating over an array named fruits in Swift?
Afor fruit in fruits { print(fruit) }
Bfor (fruit : fruits) { print(fruit) }
Cfor fruit to fruits { print(fruit) }
Dforeach fruit in fruits { print(fruit) }
Step-by-Step Solution
Solution:
  1. Step 1: Recall Swift for-in loop syntax

    The correct syntax uses 'for variable in collection { }'.
  2. Step 2: Compare options to syntax

    Only for fruit in fruits { print(fruit) } matches this syntax exactly.
  3. Final Answer:

    for fruit in fruits { print(fruit) } -> Option A
  4. Quick Check:

    Correct for-in syntax uses 'in' keyword [OK]
Quick Trick: Remember: for item in collection { } is correct [OK]
Common Mistakes:
  • Using 'to' instead of 'in'
  • Using parentheses incorrectly
  • Using 'foreach' which is not Swift syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes