LearnFrenzy
Sign in Take a free test

Control Flow Statements · Control Flow MCQ · Question 17

Which if does an else belong to? Answer, explanation & discussion

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

Without braces, in if (a) if (b) x(); else y();, which if does the else belong to?

  • A The first if (a)
  • B The nearest if (b) Correct
  • C Both
  • D It is a compile error
Correct Answer: Option B

An else binds to the nearest unmatched if – the "dangling else". Braces make intent explicit.

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