Bird
0
0

Identify the error in this Swift code:

medium📝 Debug Q6 of 15
Swift - Control Flow
Identify the error in this Swift code:
let count = 10
if count > 5
    print("Count is greater than 5")
    print("Keep counting")
else
    print("Count is 5 or less")
AVariable 'count' not declared
BUsing 'else' without an 'if'
CIncorrect comparison operator
DMissing braces {} around if and else blocks
Step-by-Step Solution
Solution:
  1. Step 1: Check syntax for if-else

    Swift requires braces {} to group multiple statements inside if or else.
  2. Step 2: Identify the problem

    Multiple print statements under if without braces cause syntax error.
  3. Final Answer:

    Missing braces {} around if and else blocks -> Option D
  4. Quick Check:

    Multiple statements need braces [OK]
Quick Trick: Use braces for multiple statements in if or else [OK]
Common Mistakes:
  • Omitting braces when multiple statements follow if or else
  • Misplacing else block
  • Using wrong operators

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes