Bird
0
0

Identify the error in this Swift code:

medium📝 Debug Q6 of 15
Swift - Loops
Identify the error in this Swift code:
var x = 3
while x > 0
    print(x)
    x -= 1
AMissing braces {} around loop body
BIncorrect variable name
CCondition syntax is wrong
DNo error, code is correct
Step-by-Step Solution
Solution:
  1. Step 1: Check Swift while loop syntax

    Swift requires braces {} to group multiple statements inside the loop body.
  2. Step 2: Identify missing braces

    Code has two statements after while but no braces, causing syntax error.
  3. Final Answer:

    Missing braces {} around loop body -> Option A
  4. Quick Check:

    Multiple statements need braces in while loop [OK]
Quick Trick: Use braces for multiple statements in while loop body [OK]
Common Mistakes:
  • Omitting braces for multiple lines
  • Assuming indentation replaces braces

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes