Nested If else Statement in C

quiz
Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Overview

If else statements are used for decision making by specifying which block of code is to be executed when a certain condition is met. Nested if-else statements are just if-else statements inside other if-else statements to provide better decision making.

To understand this topic, you should have some knowledge of the following C Programming topics:

Scope

  • This article explains the concept of Nested If Else Statement in C.
  • This article shows the implementation of nested if else in C language.

Introduction to Nested if else Statement in C

Whenever we need to make a decision about something, we ask ourselves questions, and based on the conditions, we evaluate which path to take. This is majorly the case when we have many options, and only one of them is to be picked.

In programming languages, to implement this very case, we use if-else statements. Let's look at a quick recap of if and else statements before introducing nested if else statement in C.

Recap of If Else Statements

Whenever we need to make different decisions based on specific conditions being satisfied in programming, we use if-else statements. These statements are called decision making statements due to this very reason. They help in making decisions based on conditions and hence contribute to deciding the flow of the code.

If the condition inside if is true, the statements inside the if bracket is executed. If the condition turns out to be false, the if block is skipped, and the statements inside the else block are executed.

For example, given a number we need to check if it is even or not. That means we need to make a decision of printing even or odd based on the parity of the number given. For this, we will use if and else statement.

if n is even 
    print even
else
    print odd

The actual code in C would be :

#include <stdio.h>

int main() {

  // variable to store the given number
  int n;

  //take input from the user
  scanf("%d", &n);

  //if else condition to check whether the number is even or odd
  if (n % 2 == 0) {
    //the number is even
    printf("Even");
  } else {
    //the number is odd
    printf("Odd");
  }

  return 0;
}

Input

6

Output

Even

Nested If Else Statement in C

We already saw how useful if and else statements are, but what if we need to check for more conditions even when one condition is satisfied?

For example, if we need to analyse if the number is even or odd, and then if it is even, whether it is divisible by 4 or not, and if it is odd, whether it is divisible by 3 or not. In such a case, only one if and else statement wouldn’t suffice.

First, we will check with one if-else statement whether the number is even or odd. Then in the if block, that means if the number was even, we would have to include another if and else statement checking if it is divisible by 4 or not, and similarly in the else block, we would have to include another if and else statement checking if the number is divisible by 3 or not.

Including numerous if-else statements inside an if and else statement is called nesting. The second if and else statements are said to be nested inside the first if and else statement.

This is why C language allows nesting of if and else statements. These are called nested if else statements and provide clearer decision making when some extra conditions are needed to be checked inside the initial conditions like in the previous example.

Syntax of Nested If Else Statement in C

The syntax for Nested If Else Statement in C will be :

//check if the first condition holds
if (condition 1) {
    
    //if the second condition holds
    if (condition 2) {
        do something
    }
    //if the second condition does not hold
    else {
        do something else
    }
    
}
// if the first condition does not hold
else{
    
    //if the third condition holds
    if (condition 3) {
        do something
    }
    //if the third condition does not hold
    else {
        do something else
    }
    
}

The code in C for the above described example where we need to analyse if the number is even or odd, and then if it is even, whether it is divisible by 4 or not, and if it is odd, whether it is divisible by 3 or not will be :

#include <stdio.h>

int main() {

  // variable to store the given number
  int n;

  //take input from the user
  scanf("%d", &n);

  //if else condition to check whether the number is even or odd
  if (n % 2 == 0) {

    //the number is even
    printf("Even ");

    //nested if else condition to check if n is divisible by 4 or not
    if (n % 4 == 0) {
      //the number is divisible by 4
      printf("and divisible by 4");
    } else {
      //the number is not divisible by 4
      printf("and not divisible by 4");
    }
  } else {
    //the number is odd
    printf("Odd ");

    //nested if else condition to check if n is divisible by 3 or not
    if (n % 3 == 0) {
      //the number is divisible by 3
      printf("and divisible by 3");
    } else {
      //the number is not divisible by 3
      printf("and not divisible by 3");
    }

  }

  return 0;
}

Input

14

Output

Even and not divisible by 4 

Flowchart of Nested If Else Statement in C

The flowchart for nested if-else statements is shown below in the diagram.

flowchart for nested if else statements

Working of Nested If Statement in C

How does the nested if statement provides flow control and decision making in programming? Let's look at the working of Nested If Statement in C to understand this better.

Taking the previous example, we have the first IF statement which evaluates if n is even. If this n is even, that means the expression n % 2 == 0 evaluates to true, we enter the if block. Here we have our nested if statement which evaluates if n is divisible by 4. If the expression n % 4 == 0evaluates to true, we enter the nested if statement block. Here we print that the number is even and divisible by 4. If the expression n % 4 == 0 was evaluated to be false, we enter the nested else statement and print that the number is even but not divisible by 4.

Similarly, if the expression n % 2 == 0 evaluates to false, we enter the first else block, skipping the if part as the condition is false, and we check the condition of the nested if statement. If the expression n % 3 == 0 evaluates to true, we enter the nested if statement block. Here we print that the number is odd and divisible by 3. If the expression n % 3 == 0 was evaluated to be false, we enter the nested else statement and print that the number is even but not divisible by 3.

The thing to note here is when we enter the if block, the else block is ignored and if we enter the else block, the if block is ignored, and this is exactly how if else statements help in decision making. Only a particular block of code is executed based on what conditions are being satisfied.

Nested if statement provides better decision making when other conditions are to be analyzed inside the first condition, and so we can include more than one nested if statement inside an if statement to get the required result.

Examples for Nested If Else Statement in C

We have taken a look at the syntax and working of nested if-else statements in C. Let us go through some examples to get a better idea.

Example 1 : Check if three numbers are equal

Given three numbers, we need to check if all of them are equal in value or not.

We will use nested if else statement to check this. First, we check that out of the three numbers, whether the first two are equal. If they are, then we go inside the nested if to check whether the third is equal to them. If yes, then all are equal else they are not equal. And if the first two numbers themselves are not equal to each other, no need to check further, we can simply output No.

The code in C is given below.

#include <stdio.h>

int main() {

  // variables to store the three numbers
  int a, b, c;

  //take input from the user
  scanf("%d %d %d", &a, &b, &c);

  //if else condition to check whether first two numbers are equal
  if (a == b) {

    //nested if else condition to check if c is equal to a and b
    if (a == c) {
      //all are equal
      printf("Yes");
    } else {
      //all are not equal
      printf("No");
    }
  } else {
    //the first two numbers are not equal, so they are not equal
    printf("No");
  }

  return 0;
}

Input 1

1 3 4

Output 1

No

Input 2

1 1 1

Output 2

Yes 

Example 2 : Which number is greatest among three numbers

Given three numbers, find the greatest among them and output its value.

We will use nested if else statement in C to check this. First, we check if the first number is greater than the second number. If this is true, it means that maybe the first number might be the greatest among all three. To check whether this might be the case, we go into the nested if else loop. We again check if the first number is greater than the third number, then it is the largest of them all, else the third number is the largest of them.

If the first number is smaller than the second number, we enter the else block. In this case, the second number might be the greatest of them all. To check whether this might be the case, we go into the nested if else loop. We again check if the second number is greater than the third number, then it is the largest of them all, else the third number is the largest of them.

The code in C is given below.

#include <stdio.h>

int main() {

  // variables to store the three numbers
  int a, b, c;

  //take input from the user
  scanf("%d %d %d", &a, &b, &c);

  //if else condition to check whether the first number is greater than the second
  if (a > b) {

    //nested if else condition to check if a>c
    if (a > c) {
      //a is greatest
      printf("%d", a);
    } else {
      //c is the greatest
      printf("%d", c);
    }

  } else {

    //nested if else condition to check if b>c
    if (b > c) {
      //b is greatest
      printf("%d", b);
    } else {
      //c is the greatest
      printf("%d", c);
    }

  }

  return 0;
}

Input 1

4 12 8

Output 1

12

Example 3 : Grade of a student based on marks

Given the grade of a student, find whether he/she passes or fails and if he/she passes, output the grade achieved.

First, according to the marks of the student, we need to check whether the student is pass or fail. So, the first if statement analyses whether the student passed or not. If the student passes, we next need to check what grade the student received based on his marks, for this we will use nested if-else statements. If the student fails, there is no other case possible, so we just output the F grade.

The code in C is given below.

#include <stdio.h>

int main() {

  // variable to store the marks of the student
  int marks;

  //take input from the user
  scanf("%d", &marks);

  //if else condition to check whether the student is pass
  if (marks >= 35) {

    //nested if else condition to determine the grade of the student
    if (marks >= 90) {
      //A grade
      printf("A");
    } else if (marks >= 80) {
      //B grade
      printf("B");
    } else if (marks >= 70) {
      //C grade
      printf("C");
    } else if (marks >= 60) {
      //D grade
      printf("D");
    } else {
      //E grade
      printf("E");
    }
  } else {
    //the student didn't pass
    printf("F");

  }

  return 0;
}

Input 1

95

Output 1

A

Input 2

30

Output 2

F

Input 3

45

Output 3

E

Input 4

75

Output 4

C

Example 4 : Check if a year is a leap year

Given a year, check if it is a leap year or not.

A leap year is a year that has 366 days, instead of 365 days. It has one day extra in the month of February. Leap years occur once in 4 years, so any year that is completely divisible by four should be a leap year. But this is not always true. If there is some year and it is divisible by hundred, it will be a leap year only if it is also divisible by four hundred.

This means that the first if statement will check if the given year is divisible by 4. If it is not, then it cannot be a leap year. But, if it is, then we need to check if the year is divisible by 100. If it is not divisible by 100, then it is surely a leap year. But, if it is divisible by 100, then we need to check if it is also divisible by 400. If it is, then it is a leap year, else it is not a leap year.

Looking closely at the explanation, we would have to use two nested if-else statements in this code for the desired correct solution.

The code in C is given below.

#include <stdio.h>

int main() {

  // variable to store the year
  int year;

  //take input from the user
  scanf("%d", &year);

  //if else condition to check whether the year is divisible by 4
  if (year % 4 == 0) {

    //nested if else condition to check if year is divisible by 100
    if (year % 100 == 0) {

      //nested if else condition to check if year is divisible by 400
      if (year % 400 == 0) {
        //the year is a leap year
        printf("Leap year");
      } else {
        //the year is not a leap year
        printf("Not a leap year");
      }

    } else {
      //the year is a leap year
      printf("Leap year");
    }

  } else {

    //the year is not a leap year
    printf("Not a leap year");

  }

  return 0;
}

Input 1

2000

Output 1

Leap year

Input 2

1900

Output 2

Not a leap year

Input 3

1995

Output 3

Not a leap year

Input 4

2024

Output 4

Leap year

Conclusion

  • In programming languages, if else statements are used for decision making. They determine the flow of code by specifying different operations in different cases.
  • Including numerous if-else statements inside an if and else statement is called nesting. The second if and else statements are said to be nested inside the first if and else statement.
  • When we enter the if block, the else block is ignored and if we enter the else block, the if block is ignored. Nested if else follow the same working.
  • C language allows nesting of if-else statements to facilitate better decision making.
Challenge Time!
quiz
quiz
Time to test your skills and win rewards! Note: Rewards will be credited after the next product update.
Free Courses by top Scaler instructors
certificate icon
Certificates
C Tutorial
This program includes modules that cover the basics to advance constructs of C Tutorial. The highly interactive and curated modules are designed to help you become a master of this language.'
If you’re a learning enthusiast, this is for you.
Module Certificate
Criteria
Upon successful completion of all the modules in the hub, you will be eligible for a certificate.
You need to sign in, in the beginning, to track your progress and get your certificate.
rcbGet a Free personalized Career Roadmap from