C Programming Tutorial - ITDeveloper

ITDeveloper : C Programming

C Programming

Step by Step Tutorials


Share with a Friend

Nested if Statement In C

Nested if...else

It is possible to include an if...else statement inside the body of another if...else statement.

Example : Nested if...else

This program given below relates two integers using either <, > and = similar to the if...else ladder's example. However, we will use a nested if...else statement to solve this problem.

#include <stdio.h>

int main() {

    int num1, num2;

    printf("Enter two integers: ");

    scanf("%d %d", &num1, &num2);

    if (num1 >= num2) {

      if (num1 == num2) {

        printf("Result: %d = %d",num1,num2);

      }

      else {

        printf("Result: %d > %d", num1, num2);

      }

    }

    else {

        printf("Result: %d < %d",num1, num2);

    }

    return 0;

}

OUTPUT:

Enter two integers: 5

4

Result: 5 > 4

If the body of an if...else statement has only one statement, you do not need to use brackets {}.

Example: 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;

  printf("Enter a Number : ");

  //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;

}

 

OUTPUT:

Enter a Number : 12

Even and divisible by 4

 

OUTPUT 2:

Enter a Number : 15

Odd and divisible by 3

 

OUTPUT 3:

Enter a Number : 17

Odd and not divisible by 3

Example : Check if three numbers are equal

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

#include <stdio.h>

int main() {

  // variables to store the three numbers

  int a, b, c;

  printf("Enter 3 Numbers : ");

  //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;

}

OUTPUT 1:

Enter 3 Numbers : 4 4 4

Yes

OUTPUT 2:

Enter 3 Numbers : 3 4 5

No