If else statement or if else control flow

If else statement is used to check several conditions and it is the most uses statement of any programming language. This is actually used to execute a block of code for a specific condition only. If the condition does not true then that block of code will be skipped by the compiler.

We use this if else statement in various way. There are mainly three forms of if else statement in C++. These are as follows;

  • if statement;
  • else statement;
  • else if statement;

Now let’s go ahead with the uses and functions of if else statement in C++.

Using if statement in C++

If statement is used to check a specific condition and the code inside ‘if block‘ will be executed by the compiler when the condition will be true.

Now, see the syntax of if statement for C++.

// syntax of if statement in C++

if(condition){
    // code to execute
    // these code will be executed if the condition becomes true
}

// code outside of if block

Now, let’s try to understand if statement through an executable program of C++.

#include <iostream>
using namespace std;

int main(){
    int x;
    cout << "Enter a positive or negative number here : ";
    cin >> x;

    if(x == 0){
        cout << "\nYou have entered zero.\n";
    }

    if(x > 0){
        cout << "\nThe number is positive.\n";
    }

    if(x < 0){
        cout << "\nThe number is negative.\n";
    }

    return 0;

}

In the above program if the entered number is zero then the first condition will be true. And then the code inside that block will be executed and the code inside other two blocks will be skipped by the compiler.

if-else-statement-in-c-if-statement

If the given number becomes greater than zero then the second if statement will be true and the code inside that block will be executed. Other two condition will false and the code of those block will be skipped.

if-else-condition-in-c-if-statement

Again if the number is less than zero, the third condition will be true and other conditions will be false.

if-else-statement-in-c-if-else-condition-in-c

Using else statement in C++

The code inside else statement is executed by default. else statement does not check any condition. Actually else statement is used with “if statement” to execute some code when the condition of “if statement” will be false.

Syntax of else statement is given bellow;

// syntax of else statement in C++

if(condition){
    // code to execute when condition is true
    
}else{
    // default code to execute
    // need not check any condition
}

We don’t need to use else statement separately rather they are used with if statement. Let’s be clear from the bellow program of else statement.

#include<iostream>
using namespace std;

int main(){
    int y;
    cout << "Enter a number here (positive or negative) : ";
    cin >> y;

    if(y >= 0){
        cout << "\nPositive number.\n";

    }else{
        cout << "\nNegative number.\n";
    }

    return 0;

}

In the above C++ program if the given number is greater than or equal to zero then the code inside the if statement will be executed. The code under else block will not be executed by the compiler.

Enter a number here (positive or negative) : 2

Positive number.

But if the number is not greater than or equal to zero then the code under else block will automatically be executed without checking any condition. And then the if block will be skipped.

Enter a number here (positive or negative) : -6

Negative number.

Using else if statement in C++

When we need to check more than two condition, we have to use else if statement. Because maximum two conditions can be successfully checked with if and else statement (except nesting them which we will learn on our next article).

The syntax for else if statement is given bellow;

// else if statement in C++

if(condition){
    // code to execute
    
}else if(condition){
    // code to execute when first conditon becomes false
    
}else{
    // default code to execute
}

We can not use else if statement separately without using if statement first. When the condition of “if statement” will be false only then the condition of else if statement will get chance to check. However, we can use more than one “else if statement” under one “if statement”.

Now, let’s see a C++ program using else if statement. Suppose we want to find the letter grade of a student who will give his obtained marks in the examination.

// C++ program using else if statement

#include<iostream>
using namespace std;

int main(){
    int m;
    cout << "Enter your obtained marks : ";
    cin >> m;

    if(m > 100 || m < 0){
        cout << "\nInvalid mark.\n";

    }else if(m >= 80){
        cout << "\nCongratulation! You have got GPA 5.00\n";

    }else if(m >= 70){
        cout << "\nYou have got GPA 4.00\n";

    }else if(m >= 60){
        cout << "\nYou have got GPA 3.50\n";

    }else if(m >= 50){
        cout << "\nYou have got GPA 3.00\n";

    }else if(m >= 40){
        cout << "\nYou have got GPA 2.00\n";

    }else if(m >= 33){
        cout << "\nYou have got GPA 1.00\n";

    }else{
        cout << "\nOOPs! You are failed.\n";
    }

    return 0;

}

You should bear in mind that the condition of “else if block” will be checked sequentially. When one condition will be true, other condition will not be checked and these are skipped by the compiler.

When you enter marks greater than 100 or less than 0, then the first condition will be true. So, the program will be end executing the code under if statement.

Enter your obtained marks : 124

Invalid mark.

If you enter a marks 56 then the first four condition will be false and the fifth condition will be true. Other statements bellow the fifth statement will be skipped.

Enter your obtained marks : 56

You have got GPA 3.00

When all the condition is checked and find false, then the default code of else block will be executed.

Enter your obtained marks : 29

OOPs! You are failed.

However, we can use nested if else statement to check multiple conditions more efficiently which we will learn in our next article.

Previous pageC++ type casting

Next pagenested if else in C++