What is Nested if-else with an example:

Share Your Love

A nested if-else statement in C programming is an if statement that is another if-else statement inside it. And Inside else statement also another if-else statement present. Yes, both C and C++ allow us to nested if-else statements within if statements if-else, that is, we can place an if-else statement inside another else statement.

A program to check whether a number is positive, negative or zero. If positive to check whether the number is odd or even, demonstrate the use of nested if-else.

#include<stdio.h>//header file for standard input and output
int main()//main function initialised as integer
{//main function braces opened
	int num;//variable number initialised as integer to take user input
	printf("Enter the number you want to check");//print message
	scanf("%d",&num);//user input
	if(num>0)//test case to check if number is greater than 0
	{//if braces opened
		printf("\nThe number is positive");//positive message 
		if(num%2==0)//test case to check even or odd
			printf("\nThe number is even");//print message
		else//else case (if not satisfied)
			printf("\nThe number is odd");//print message
	}//if braces closed
	else if(num==0)//else if case where number is equal to zero
	{//braces opened
		printf("\nThe number is equals to zero");//print message 
	}//braces closed 
	else//else case if above cases not satisfied
	{//braces opened 
		printf("\nThe number is negative");//print message
	}//braces closed 
	return 0;//execution success
}//main function braces closed

Explanation Of whether a number is positive, negative or zero. If positive to check whether the number is odd or even:

  1. Header file for standard input and output.
  2. The main function initialised as an integer.
  3. The main function braces opened.
  4. The variable number initialised as integer to take user input.
  5. print message on a console for input purposes.
  6. user input through scanf().
  7. test case to check if a number is greater than 0.
  8. if braces opened.
  9. positive message.
  10. test case to check even or odd.
  11. print message if even.
  12. print message if odd.
  13. nested if braces closed.
  14. else if the case where number is equal to zero.
  15. braces opened In else-if after successful check if the number is zero.
  16. print message the number is zero.
  17. braces closed In else-if after successful check if the number is zero.
  18. else case if above cases, not satisfied.
  19. braces opened if all the cases are false.
  20. print message the number is negative.
  21. braces closed if all the cases are false.
  22. execution success.
  23. main function braces close.

GU47Dp l9MJ1NE6pDrVICnPZHQpsB3Mp5O0U3

This was an example of nested if….else decision making in C, hope you like it and have cleared your doubts.

Please write comments or WhatsApp if you find anything incorrect, or you want to share more information about the topic discussed above.

Share Your Love
Avatar photo
Soham Malakar

Hello my name is Soham Malakar am currently pursuing BCA

Coding is one of my favorite hobbies.
I love to code and an enthusiast to learn different types of programming languages.
I love to get intoxicated in the world of technology and video games.
A quick learner possessing better results.

Articles: 40

Newsletter Updates

Enter your email address below to subscribe to our newsletter