Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does a C-like compiler interpret the if statement

In C-like languages, we are used to having if statements similar to the following:

if(x == 5) {
    //do something
}
else if(x == 7) {
    //do something else
}
else if(x == 9) {
    //do something else
} else {
    //do something else
}

My question is, does the compiler see that if statement that way, or does it end up being interpreted like:

if(x == 5) {
    //do something
}
else {
    if(x == 7) {
        //do something
    }
    else {
        if(x == 9) {
            //do something
        }
        else {
            //do something else
        }
    }
}

EDIT: I realized that while the question made sense in my head, it probably sounded rather stupid to the rest of the general populace. I was more referring to how the AST would look and if there was any special AST cases for 'else-if' statements or if it would be compiled as a cascading if/else block.

like image 930
Damon Swayn Avatar asked Jun 09 '13 02:06

Damon Swayn


People also ask

How does if statement work in C?

C has the following conditional statements: Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.

How is an if statement used in programming?

The IF statement is a decision-making statement that guides a program to make decisions based on specified criteria. The IF statement executes one set of code if a specified condition is met (TRUE) or another set of code evaluates to FALSE.

What is if statement in C Explain with syntax and example?

Syntax. If the Boolean expression evaluates to true, then the block of code inside the 'if' statement will be executed. If the Boolean expression evaluates to false, then the first set of code after the end of the 'if' statement (after the closing curly brace) will be executed.

What is the purpose of IF statement explain with an example?

Syntax. Use the IF function, one of the logical functions, to return one value if a condition is true and another value if it's false. For example: =IF(A2>B2,"Over Budget","OK")


1 Answers

They are equivalent to a C compiler. There is no special syntax else if in C. The second if is just another if statement.


To make it clearer, according to C99 standard, if statement is defined as

selection-statement:
    if (expression) statement
    if (expression) statement else statement
    switch (expression) statement

and a compound-statement is defined as

compound-statement:
    {block-item-list(opt) }
block-item-list:
    block-item
    block-item-list block-item
block-item:
    declaration
    statement

When a compiler frond-end tries to understand a source code file it often follows these steps:

  1. Lexical analysis: turn the plain-text source code into a list of 'tokens'
  2. Semantic analysis: parse the token list and generate an abstract syntax tree (AST)

The tree is then passed to compiler middle-end (to optimize) or back-end (to generate machine code)

In your case this if statement

if(x == 7) {
    //do something else
} else if(x == 9) {
    //do something else
} else {
    //do something else
}

Is parsed as a selection-statement inside a selection-statement,

    selection-stmt
    /     |      \
 exp     stmt     stmt
  |       |        |
 ...     ...    selection-stmt
                /      |      \
              exp     stmt    stmt
               |       |       |
              ...     ...     ...

and this one

if(x == 7) {
    //do something else
} else {
    if(x == 9) {
        //do something else
    } else {
        //do something else
    }
}

is the same selection-statement inside a compound-statement inside a selection-statement:

    selection-stmt
    /     |      \
 exp     stmt     stmt
  |       |        |
 ...     ...    compound-stmt
                      |
                block-item-list
                      |
                  block-item
                      |
                     stmt
                      |
                selection-stmt
                /      |      \
               exp    stmt    stmt
                |      |       |
               ...    ...     ...

So they have different ASTs. But it makes no differences for the compiler backend: as you can see in the AST, there is no structural changes.

like image 101
Naruil Avatar answered Oct 26 '22 22:10

Naruil