Home > Enterprise >  If statement without curly braces
If statement without curly braces

Time:01-16

been looking at this one problem I came across. The question is what does the following snippet return.

int main()
{
    int a = 1, b = 2, c = 3, d = 4;
    int x = a;
    if (a > b)
    if (b < c) x = b;
    else x = c;
    return(x);
}

As I understand this, if statements without a curly brace are allowed and will execute the immediate statement following it. So in the case of the first condition (a > b), if true, then the following line will execute as the statement. And then if the nested if statements' condition (b < c) is also true, then the statement following it will be executed. And the else statement would "belong" to the first if statement. However, putting this into a compiler shows me that the else statement actually belongs to the nested if statement and returns 1.

Could someone explain what I am misunderstanding? Does the else statement, in the case without any curly braces, also belong to the closest if statement?

And yes, I understand this is a poorly written piece of code with the readability out the window. I would not do this in practice but still am curious about the correct way to interpret this program.

I ran the program and saw that it returns 1, which indicates to me that the else statement is actually part of the nested if statement, which contradicts the fact if statements without curly braces execute only the line immediately following it.

CodePudding user response:

The C standard specifically states such a case:

6.8.4.1 The if statement Constraints

1 The controlling expression of an if statement shall have scalar type.

Semantics

2 In both forms, the first substatement is executed if the expression compares unequal to 0. In the else form, the second substatement is executed if the expression compares equal to 0. If the first substatement is reached via a label, the second substatement is not executed.

3 An else is associated with the lexically nearest preceding if that is allowed by the syntax.

The standard says that the if statement executes a following substatement, not a "line", and else should match with closest if.

CodePudding user response:

if (b<c) x=b;
else x=c;

Above is a single if-else statement, from the point of view of the outer if. Indeed else is specified to belong to the closest if it can.

So the outer if either skips this statement, or not. Nothing special is happening here

Note that using curly braces also creates a single compound statement. if always either executes or skips a single statement, even in that case.

It's just that a statement can have multiple statements in it.

CodePudding user response:

The exact syntax of an if statement, according to section 6.8.4p1 of the C standard is one of:

  • if ( expression ) statement
  • if ( expression ) statement else statement

So when parsing the following:

if (a>b)
if (b<c) x=b;
else x=c;

The first if starts an if statement, then (a>b) matches the parenthesized expression. The next thing the parser looks for is a statement. The second if that follows is the start of such a statement, which continues with the parenthesized expression (b<c).

Now the parser is looking for a statement to match the inner if, and the statement expression x=b; matches that. At this point, we don't know if the inner if statement has been completed yet because it could be followed by an else. Indeed we see that the keyword else follows, so the parsing of the inner if statement continues, and the statement expression x=c; satisfies the expected statement.

At this point, the parsing of the inner if is complete, which also completes parsing of the outer if.

So to summarize, an else will always be matched with the innermost if it can potentially be matched with.

  • Related