if-else Statement (C++)

Controls conditional branching. Statements in the if-block are executed only if the if-expression evaluates to a non-zero value (or true). If the value of expression is nonzero, statement1 and any other statements in the block are executed and the else-block, if present, is skipped. If the value of expression is zero, then the if-block is skipped and the else-block, if present, is executed. Expressions that evaluate to non-zero are

  • true
  • a non-null pointer,
  • any non-zero arithmetic value, or
  • a class type that defines an unambiguous conversion to an arithmetic, boolean or pointer type. (For information about conversions, see Standard Conversions.)

Syntax


if ( expression )  
{
   statement1;
   ...  
}
else  // optional
{
   statement2;
   ...
} 

// Visual Studio 2017 version 15.3 and later:
if ( initialization; expression )  
{
   statement1;
   ...  
}
else  // optional
{
   statement2;
   ...
}  

// Visual Studio 2017 version 15.3 and later:
if constexpr (expression)
{
    statement1;
    ...
}
else  // optional
{
   statement2;
   ...
} 

Example

// if_else_statement.cpp  
#include <iostream>

using namespace std;

class C
{
    public:
    void do_somthing(){}
};
void init(C){}
bool is_true() { return true; }
int x = 10;

int main()
{
    if (is_true())
    {
        cout << "b is true!\n";  // executed
    }
    else
    {
        cout << "b is false!\n";
    }

  // no else statement
    if (x == 10)
    {
        x = 0; 
    }


    C* c;
  init(c);
    if (c)
    {
        c->do_something();
    }
    else
    {
        cout << "c is null!\n";
    }
}

if statement with an initializer

Visual Studio 2017 version 15.3 and later (available with /std:c++17): An if statement may also contain an expression that declares and initializes a named variable. Use this form of the if-statement when the variable is only needed within the scope of the if-block.

## Example  
#include <iostream>
#include <mutex>
#include <map>
#include <string>
#include <algorithm>


using namespace std;

map<int, string> m;
mutex mx;
bool shared_flag; // guarded by mx
void unsafe_operation() {}

int main()
{

    if (auto it = m.find(10); it != m.end())
    {
        cout << it->second;
        return 0;
    }

    if (char buf[10]; fgets(buf, 10, stdin))
    {
        m[0] += buf;
    }

    if (lock_guard<mutex> lock(mx); shared_flag)
    {
        unsafe_operation();
        shared_flag = false;
    }


    string s{ "if" };
    if (auto keywords = { "if", "for", "while" }; any_of(keywords.begin(), keywords.end(), [&s](const char* kw) { return s == kw; }))
    {
        cout << "Error! Token must not be a keyword\n";
    }

}

In all forms of the if statement, expression, which can have any value except a structure, is evaluated, including all side effects. Control passes from the if statement to the next statement in the program unless one of the statements contains a break, continue, or goto.

The else clause of an if...else statement is associated with the closest previous if statement in the same scope that does not have a corresponding else statement.

constexpr if statements

Visual Studio 2017 version 15.3 and later (available with /std:c++17): In function templates, you can use a constexpr if statement to make compile-time branching decisions without having to resort to multiple function overloads. For example, you can write a single function that handles parameter unpacking (no zero-parameter overload is needed):

template <class T, class... Rest>
void f(T&& t, Rest&&... r)
{
// handle t
   do_something(t);

   // handle r conditionally
   constexpr if (sizeof...(r)) 
   {

      f(r...); 
   }
   else
   {
       g(r...);
   }
}

See Also

Selection Statements
Keywords
switch Statement (C++)