C++ if, else-if Statement

This tutorial will teach you about the usage of if and else-if statements in the C++ programming language, along with code samples.

The if Statement in C++ Language

The if statement is one of the simple conditional statements. The syntax of if statement is:

if (expression)
   statement;
  • The expression is any valid C++ expression, also known as conditional expression.
  • The expression evaluates to either true or false.
  • If expression is evaluated to 0, it is false and if non-zero, the expression is evaluated as true.
  • The statement is only executed if the result of the expression is true.
if ( 80 > 50 )
   cout << "Condition is true, 80 is greater than 50";

// Output: Condition is true, 80 is greater than 50

The expression 80 > 50 is evaluated and the message is only displayed when the result of the expression is true. As 80 is greater than 50, the statement is executed and message is displayed.

int x = 1;
if(x)
  cout << "Condition is true";

// Output: Condition is true

Here, x is not zero, expression is evaluated to true, the statement is executed. Let’s try above code in another way:

int x = 1;
if(x!=0)
   cout << "Condition is true";

// Output: Condition is true

The value of x in above example is not equals to zero, expression is evaluated to true, the statement is executed.

In the above example only one statement is executed, if the expression is evaluated to true. A code block with multiple statements may also be executed enclosed in curly braces { }.

if(expression)
{
  statement 1;
  statement 2;
  statement 3;
}

Let’s try if condition with block of multiple statements.

int x = 1;
if(x) {
   cout << "Condition is true";
  return;
}

In the above example, x is true. In this case, the block of code enclosed in braces { } is executed.

The if else Statement in C Language

In the above section, you learned about if statement, where one statement or block of statements is executed if expression is true. What if you want to execute a specific statement or block of statements in case expression is false. The if-else statement solves such problems. The syntax of the if-else is:

if(expression)
   statement1;
else
   statement2;

The if-else statement has following flow:

  • If the expression is true, only the statement or block of statements following the if keyword is executed.
  • If the expression is false, the control is transferred to statement or block of statements following the else keyword.
  • else part is optional.
int marks = 80;
if (marks > 90)

   // This statement is executed, if expression is true
   cout << "You get A grade";
else

   // This statement is executed, if expression is false
  cout << "You didn't get A grade";

// Output: You didn't get A grade

In the above example, value of variable marks is 80. The expression is evaluated to false. The control is transferred to statement following the else keyword. In this case text “You didn’t get A grade” is printed.

The Nested if in C++ Language

You can nest the if statements to any depth required. Nesting means one if statement is part of body of another if statement.

// Outer if condition
if (expression1)
   // Inner if condition
   if (expression2)
      statement; 

First expression1 is evaluated, if result is true, then expression2 is evaluated. The control will not be transferred to inner if condition if expression of outer if condition is evaluated to false. The statement will not be executed unless both expression1 and expression2 are true.

int x = 7;
    if(x > 5)
        if (x < 10)
           cout << "Value of x is between 5 and 10";

\\ Output: Value of x is between 5 and 10

Here, value of x is 7. the expression x>5 of outer if condition is evaluated to true. The control is transferred to inner if condition. the expression x<10 of inner if condition is evaluated to true. The cout statement is executed.

See following example, when outer if evaluates to false.

int x = 3;
    if(x > 5)
        if (x < 10)
            cout << "Value of x is between 5 and 10";

In the above example, value of x is 3. The expression x>5 of outer if is evaluated to false. In this case, the control is not transferred to inner if condition and statement is also not executed.

You can also use optional else statement in nested if. But the confusion is, else is associated with which if condition, outer if or inner if? The answer, else statement is associated to the nearest previous (else-less) if. See example below:

int x = 15;
    if(x > 5)
        if (x < 10)
            cout << "Value of x is between 5 and 10";
        else
            cout << "Value of x is greater than 10";

Here, the value of x is 15. The expression of outer if evaluates to true, control is transferred to inner if. The expression of inner if evaluates to false, control is transferred to its associated else statement. The else is associated with if (x < 10 ), which is its nearest previous if.

You can use braces { } to associate else with outer if statement, as the following example shows:

int x = 1;
    if(x > 5){
        if (x < 10)
            cout << "Value of x is between 5 and 10";
    }
    else
       cout << "Value of x is less than 5";

// Output: Value of x is less than 5

Following example shows the else statements associated with their respective outer and inner if statements.

int x = 1;
    if(x > 5)
        if (x < 10)
            cout << "Value of x is between 5 and 10";
        else
            cout << "Value of x is greater than 10";
    else
        cout << "Value of x is less than 5";

Here, the value of x is 1. The expression of outer if evaluates to false. The control is transferred to its associated else statement and text “Value of x is less than 5” is printed.

The else-if Statement in C++ Language

The else-if statments can be nested. The last else is optional and executes when all previous expressions are false. The syntax of else-if construct is as follow:

if (expression1){
   statements;
}
else if (expression2){
   statements;
}
else {
   statements;
}

The following else-if code displays the grade depending upon the value of variable "marks". 

int marks = 75;

if (marks > 90){
  cout << "A Grade";
}else if (marks > 80){
  cout << "B Grade";
}else if (marks > 70){
  cout << "C Grade";
}else if (marks > 60){
  cout << "D Grade";
}else{
  cout << "Fail";
}  

//Output: C Grade