Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

AFAIK, if an "if" block is not provided the curly braces then only 1 statement is considered inside it. e.g.

if(..)
  statement_1;
  statement_2;

Irrespective of tabs, only statement_1 is considered inside the if block.

Following code doesn't get along with that:

int main ()
{
  if(false)  // outer - if
    if(false)  // nested - if
      cout << "false false
";
  else if(true)
    cout << "true
";
}

Above code doesn't print anything. It should have printed "true".
It appears as of the else if is automatically nested inside the outer if block. g++ -Wall issues warning, but that is not the question here. Once you put the curly braces, everything goes fine as expected.

Why such different behavior ?
[GCC demo: without braces and with braces].

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
275 views
Welcome To Ask or Share your Answers For Others

1 Answer

The behaviour isn’t actually different, it’s entirely consistent: the whole inner if block – including else if –?is considered as one block.

This is a classical ambiguity in parsing, known as the “dangling-else problem”: there are two valid ways of parsing this when the grammar is written down in the normal BNF:

Either the trailing else is part of the outer block, or of the inner block.

Most languages resolve the ambiguity by (arbitrarily) deciding that blocks are matched greedily by the parser – i.e. the else [if] is assigned to the closest if.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...