if statement - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# if statement

From cppreference.com
< c‎ | language

Conditionally executes code.

Used where code needs to be executed only if some condition is true.

### [edit] Syntax

`if (` expression `)` statement_true | (1) |
`if (` expression `)` statement_true `else` statement_false | (2) |

### [edit] Explanation

expression must be an expression of any scalar type.

If expression compares not equal to the integer zero, statement_true is executed.

In the form (2), if expression compares equal to the integer zero, statement_false is executed.

As with all other selection and iteration statements, the entire if-statement has its own block scope:
    enum {a, b};
    int different(void)
    {
        if (sizeof(enum {b, a}) != sizeof(int))
            return a; // a == 1
        return b; // b == 0 in C89, b == 1 in C99
    }
| (since C99)

### [edit] Notes

The `else` is always associated with the closest preceding `if` (in other words, if statement_true is also an if statement, then that inner if statement must contain an `else` part as well):

    int j = 1;
    if (i > 1)
       if(j > 2)
           printf("%d > 1 and %d > 2\n", i, j);
        else // this else is part of if(j>2), not part of if(i>1)
           printf("%d > 1 and %d <= 2\n", i, j);

If statement_true is entered through a goto, statement_false is not executed.

### [edit] Keywords

`if`, `else`

### [edit] Example

Run this code

    #include <stdio.h>

    int main(void)
    {
        int i = 2;
        if (i > 2) {
            printf("first is true\n");
        } else {
            printf("first is false\n");
        }

        i = 3;
        if (i == 3) printf("i == 3\n");

        if (i != 3) printf("i != 3 is true\n");
        else        printf("i != 3 is false\n");
    }

Output:

    first is false
    i == 3
    i != 3 is false

### [edit] References

- C11 standard (ISO/IEC 9899:2011):

  - 6.8.4.1 The if statement (p: 148-149)

- C99 standard (ISO/IEC 9899:1999):

  - 6.8.4.1 The if statement (p: 133-134)

- C89/C90 standard (ISO/IEC 9899:1990):

  - 3.6.4.1 The if statement

### [edit] See Also

C++ documentation for if statement

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=c/language/if&oldid=76147"