Machine-translated by Google. Questions are authored in English.
Top

Discussion

The classic dangling else problem arises because

  • A.the else clause is allowed to contain statements of any kind whatever
  • B.an else can be attached to either of two enclosing if statements
  • C.the else keyword is optional in most programming languages
  • D.the scanner cannot distinguish else from other keywords

Answer: B

Correct answer: an else can be attached to either of two enclosing if statements

With a grammar that allows an if without an else, the sentence with two ifs and one else has two parse trees: the else may belong to either. Languages resolve it by rule - the else matches the nearest unmatched if - and grammars encode that by splitting the statement nonterminal into matched and unmatched forms.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.