LearnFrenzy
Sign in Take a free test

Control Instructions · Control Instructions MCQ · Question 3

Dangling else: which if does the else belong to? Answer, explanation & discussion

  • MCQ
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

In if (a) if (b) s1; else s2; (no braces), which if does the else belong to?

  • A if (a)
  • B if (b) – the nearest unmatched if Correct
  • C Both
  • D It is a syntax error
Correct Answer: Option B

An else always pairs with the closest preceding if that has no else yet. Indentation has no meaning to the compiler; add braces to say what you mean.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links