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 present.

### [edit] Syntax

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

### [edit] Explanation

expression shall be an expression, convertible to bool.

If it evaluates to true, control is passed to statement_true, statement_false (if present) is not executed.

Otherwise, control is passed to statement_false, statement_true 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

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