if statement - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# if statement

From cppreference.com
< cpp‎ | language

Conditionally executes code.

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

### [edit] Syntax

attr(optional) `if (` condition `)` statement_true |  |
attr(optional) `if (` condition `)` statement_true `else` statement_false |  |

attr(C++11) | - | any number of attributes
condition | - | any expression which is contextually convertible to bool or a declaration of a single non-array variable with a brace-or-equals initializer.
statement_true | - | any statement often a compound statement, which is executed if condition evaluates to true
statement_false | - | any statement often a compound statement, which is executed if condition evaluates to false

### [edit] Explanation

If the condition yields true, statement_true is executed.

If the else part of the if statement is present and condition yields false, statement_false is executed.

In the second form of if statement (the one including else), if statement_true is also an if statement then that inner if statement must contain an else part as well (in other words, in nested if-statements, the else is associated with the closest if that doesn't have an else)

### [edit] Notes

If statement_true or statement_false is not a compound statement, it is treated as if it was:

    if(x)
        int i;
    // i is no longer in scope

is the same as

    if(x) {
        int i;
    } // i is no longer in scope

The scope of the name introduced by condition, if it is a declaration, is the same as the scope of the body of the statements:

    if (int x = f()) {
        int x; // error: redeclaration of x
    }
    else {
        int x; // error: redeclaration of x
    }

If statement_true is entered by goto or longjmp, statement_false is not executed. | (since C++14)

### [edit] Keywords

`if`, `else`

### [edit] Example

The following example demonstrates several usage cases of the `if` statement

Run this code

    #include <iostream>

    int main()
    {
        // simple if-statement with an else clause
        int i = 2;
        if (i > 2) {
            std::cout << i << " is greater than 2\n";
        } else {
            std::cout << i << " is not greater than 2\n";
        }

        // nested if-statement
        int j = 1;
        if (i > 1)
            if(j > 2)
                std::cout << i << " > 1 and " << j << " > 2\n";
            else // this else is part of if(j>2), not part of if(i>1)
                std::cout << i << " > 1 and " << j << " <= 2\n";

       // declarations can be used as conditions with dynamic_cast
       struct Base {
            virtual ~Base() {}
       };
       struct Derived : Base {
           void df() { std::cout << "df()\n"; }
       };
       Base* bp1 = new Base;
       Base* bp2 = new Derived;

       if(Derived* p = dynamic_cast<Derived*>(bp1)) // cast fails, returns NULL
           p->df();  // not executed

       if(auto p = dynamic_cast<Derived*>(bp2)) // cast succeeds
           p->df();  // executed
    }

Output:

    2 is not greater than 2
    2 > 1 and 1 <= 2
    df()

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