Signup/Sign In

Decision making in C++ - if, else and else if

Decision making is about deciding the order of execution of statements based on certain conditions or repeat a group of statements until certain specified conditions are met. C++ handles decision-making by supporting the following statements,

  • if statement
  • switch statement
  • conditional operator statement
  • goto statement

Decision making with if statement

The if statement may be implemented in different forms depending on the complexity of conditions to be tested. The different forms are,

  1. Simple if statement
  2. if....else statement
  3. Nested if....else statement
  4. else if statement

Simple if statement

The general form of a simple if statement is,

if(expression)
{
    statement-inside;
}
    statement-outside;

If the expression is true, then 'statement-inside' will be executed, otherwise 'statement-inside' is skipped and only 'statement-outside' will be executed.

Example:

#include< iostream.h>
int main( )
{
    int x,y;
    x=15;
    y=13;
    if (x > y )
    {
        cout << "x is greater than y";
    }
}

x is greater than y


if...else statement

The general form of a simple if...else statement is,

if(expression)
{
    statement-block1;
}
else
{
    statement-block2;
}

If the 'expression' is true or returns true, then the 'statement-block1' will get executed, else 'statement-block1' will be skipped and 'statement-block2' will be executed.

Example:

void main( )
{
    int x,y;
    x=15;
    y=18;
    if (x > y )
    {
        cout << "x is greater than y";
    }
    else
    {
        cout << "y is greater than x";
    }
}

y is greater than x


Nested if....else statement

The general form of a nested if...else statement is,

if(expression)
{
    if(expression1)
    {
        statement-block1;
    }
    else 
    {
        statement-block2;
    }
}
else
{
    statement-block3;
}

if 'expression' is false or returns false, then the 'statement-block3' will be executed, otherwise execution will enter the if condition and check for 'expression 1'. Then if the 'expression 1' is true or returns true, then the 'statement-block1' will be executed otherwise 'statement-block2' will be executed.

Example:

void main( )
{
    int a,b,c;
    cout << "enter 3 number";
    cin >> a >> b >> c;
    if(a > b)
    {
        if( a > c)
        {
            cout << "a is greatest";
        }
        else 
        {
            cout << "c is greatest";
        }
    }
    else
    {
        if( b> c)
        {
            cout << "b is greatest";
        }
        else
        {
            cout << "c is greatest";
        }
    }
}

The above code will print different statements based on the values of a, b and c variables.


else-if Ladder

The general form of else-if ladder is,

if(expression 1)
{
    statement-block1;
}
else if(expression 2) 
{
    statement-block2;
}
else if(expression 3 ) 
{
    statement-block3;
}
else 
    default-statement;

The expression is tested from the top(of the ladder) downwards. As soon as the true condition is found, the statement associated with it is executed.

Example:

void main( )
{
    int a;
    cout << "enter a number";
    cin >> a;
    if( a%5==0 && a%8==0)
    {
        cout << "divisible by both 5 and 8";
    }  
    else if( a%8==0 )
    {
        cout << "divisible by 8";
    }
    else if(a%5==0)
    {
        cout << "divisible by 5";
    }
    else 
    {
        cout << "divisible by none";
    }
}

If you enter value 40 for the variable a, then the output will be:

divisible by both 5 and 8


Points to Remember

  1. In if statement, a single statement can be included without enclosing it into curly braces { }.
    int a = 5;
    if(a > 4)
        cout << "success";
    

    success

    No curly braces are required in the above case, but if we have more than one statement inside if condition, then we must enclose them inside curly braces otherwise only the first statement after the if condition will be considered.

    int a = 2;
    if(a > 4)
        cout << "success";
        // below statement is outside the if condition
        cout << "Not inside the if condition"
    

    Not inside the if condition

  2. == must be used for comparison in the expression of if condition, if you use = the expression will always return true, because it performs assignment not comparison.
  3. Other than 0(zero), all other positive numeric values are considered as true.
    if(27)
        cout << "hello";
    

    hello