c++

C++ if, if-else and nested if-else

In this tutorial, we are going to learn about decision making statement in C++ program using different forms of if...else.

In programming, the use of if statement is to run a particular block of code only when certain condition is met.

For example, eligibility for a driving license based on age :

Here above or equal to 18 and below 18 are conditions and based on these condition eligible or ineligible is assigned.

Basically, there are three forms of if-else

  1. only if statement
  2. if...else statement
  3. Nested if..else statement

C++ if statement

Syntax of the if statement is given following.

if (condition) {
    // body of if statement
}

The if statement evaluates the condition given inside parentheses ().

Note : we write code inside body of if statement.

working of if statement in both conditions

Now let’s look at some examples.

Example:

Following program checks a number, whether it is positive.

#include <iostream>
using namespace std; 
int main() { 
    int num; 
    cout << "Enter an integer: "; 
    cin >> num;
    if (num >= 0) { 
        cout << "You entered a positive integer: " << num << endl; 
    } return 0; 
}

Output 1:

Suppose we have entered integer as 2, i.e. num = 2.

Enter an integer: 2 
You entered a positive integer: 2

In the above program, we have the condition num >= 0 If we enter the number greater or equal to 0, then the condition evaluates true. Here, we enter 2. So, the condition is true. Hence, the statement inside the body of if is executed.

Output 2:

Suppose we have entered integer as -2, i.e. num = -2.

Enter an integer: -2

In the above program, condition of if statement evaluated to false. Hence, body of if skipped.

C++ if...else

The if can have optional keyword else. Its syntax is given following,

if (condition) { 
    // block of code if condition is true 
} 
else { 
    // block of code if condition is false 
}

working of if...else statement in both conditions

If the condition evaluates true,

If the condition evaluates false,

Let’s take some examples of if...else.

Example:

The following program checks whether a number is positive or negative, taking 0 (zero) as positive number.

#include <iostream>
using namespace std; 
int main() {
    int number;
    cout << "Enter an integer: "; 
    cin >> num; 
    if (num >= 0) { 
        cout << "You entered a positive integer: " << num << endl; 
    } 
    else { 
        cout << "You entered a negative integer: " << num << endl; 
    } 
    return 0; 
}

Output 1:

Suppose we have entered integer as 3, i.e. num = 3.

Enter an integer: 3 
You entered a positive integer: 3

In the above program, we have the condition num >= 0 If we enter the number greater or equal to 0, then the condition evaluates true. Here, we enter 3. So, the condition is true. Hence, the statement inside the body of if is executed.

Output 2:

Suppose we have entered integer as -3, i.e. num = -3.

Enter an integer: -3 
You entered a negative integer: -3.

Now in this case condition of if becomes false, hence is it body has been skipped, and body of else is executed.

C++ if...else if...else statement

The if...else statement is used to execute a block of code among two alternatives. However, if we need to make a choice between more than two alternatives, we use the if...else if...else statement.

Following is the syntax of if...else if...else statement.

if (condition1) { 
    // code 1 
} 
else if (condition2){ 
    // code 2 
} 
else { 
    // code 3 
}

Here in this example:

working of if...else if...else statement in both conditions

Note : There can be more than one else if statements, but there must be only one if and else statement.

Now, let’s take an example.

Examole:

The following program checks whether a number is positive, negative or zero.

#include <iostream> 
using namespace std; 
int main() { 
    int num; 
    cout << "Enter a number: "; 
    cin >> num; 
    if (num > 0) { 
        cout << "You entered a positive number: " << num<< endl; 
    } 
    else if (num < 0) { 
        cout << "You entered a negative number: " << num << endl; 
    } 
    else { 
        cout << "You entered 0." << endl; 
    } 
    return 0; 
}

Suppose that we enter 3, -5 and 0 one by one respectively.

Output 1:

Suppose we have entered integer as 3, i.e. num = 3.

Enter an integer: 3 
You entered a positive integer: 3

Output 2:

Suppose we have entered integer as -5, i.e. num = -5.

Enter an integer: -5
You entered a positive integer: -5

Output 3:

Suppose we have entered integer as 0, i.e. num = 0.

Enter an integer: 0 
You entered 0

In the above program if number is greater than 0, code inside body of if is executed. If number is smaller than 0 , code inside body of else if is executed. Otherwise, the code inside body of else is executed.

C++ Nested if...else

In some cases, we need if statement inside another if. This is known as nested if.

The syntax is given below.

// outer if statement 
if (condition1) { 
    // statements 
    
    // inner if statement 
    if (condition2) { 
        // statements 
    } 
}

Nested if acts as multiple layered if statement, consisting of upper layer and inner layer like in above syntax.

Note :

Let’s understand it with an example.

Example:

The below program finds if an integer is odd or even or nothing.

#include <iostream> 
using namespace std; 
int main() { 
    int num; 
    cout << "Enter an integer: "; 
    cin >> num; 
    
    // outer if condition 
    if (num != 0) { 
        // inner if condition 
        if ((num % 2) == 0) { 
            cout << "The number is even." << endl; 
        } 
        // inner else condition
        else { 
            cout << "The number is odd." << endl; 
        } 
    } 
    // outer else condition 
    else { 
        cout << "The number is 0 and it is neither even nor odd." << endl; 
    } 
    return 0; 
}

Let’s suppose that we have entered integer as 16, 17 and 0 one by one respectively.

Output 1:

Suppose we have entered integer as 16, i.e. num = 16.

Enter an integer: 16
The number is even.

Output 2:

Suppose we have entered integer as 17, i.e. num = 17.

Enter an integer: 17
The number is odd.

Output 3:

Suppose we have entered integer as 0, i.e. num = 0.

Enter an integer: 0
The number is 0 and it is neither even nor odd.