if statement

From cppreference.com
< cpp‎ | language

Conditionally executes another statement.

Used where code needs to be executed based on a run-time condition.

Contents

[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 - one of
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 after conversion to bool, statement_true is executed.

If the else part of the if statement is present and condition yields false after conversion to bool, 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 were:

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

#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()

[edit] See Also

C documentation for if statement