In this post, we will learn with examples how to use nested if else statement in C programming.
Before understanding nested if else, it is very important for you to have knowledge of simple if else statement, so if you have not read our previous tutorial then definitely read it first.
When you use if else inside the if else statement, then we call it nested if else statement.
With the help of nested if else statements, we can put a condition inside the condition i.e. When you have to put such logic in your program that the second condition is checked when the first condition becomes true then you will use nested if else.
In simple if statement we learned that if this one condition is true then execute these statements i.e. if it is so then do this work.
But sometimes in programs we have to check more than one condition to run the statements and for this we use nested if else statement.
You can also read: IF Else Statement In C Programming
Contents
Nested if else statement in C Programming syntax:
if( condition ) // Outer if condition statement
{
if( condition ) // Inner (Nested) if conditional statement
{
//Statements1;
}
else //Inner else of inner if
{
//Statement2;
}
}
else //Outer else of outer if
{
if( condition ) // Inner (Nested) if statement
{
//Statement3;
}
else //Inner else of inner if
{
//Statement4;
}
}
Nested IF Else Statement in C Programming
Syntax Explanation:
We have used single line comments many times in the above nested if else syntax and we have done this only to explain the syntax.
As you can see in above syntax, we have used nested (inner) if else in both outer if and else.
But it is not necessary that you use nested if else in both if and else. If you want, you can use nested if else only in if or else or both according to your program need.
Similarly, according to the logic of your program, you can use nested if else in inner if or else or both again.
Nested IF Else Statement in C Programming
Nested if else statement example program 1:
#include<stdio.h>
#include<conio.h>
void main ()
{
int x;
clrscr();
printf("Please enter a number:")
scand("%d"&x);
if(x>9)
{
if(x%2==0)
{
printf("%d is an even number.",x);
}
else
{
printf("%d is an odd number.",x);
}
}
else
{
printf("%d is a single digit number.",x);
}
getch();
}
Nested IF Else Statement in C Programming
Output 1:
Please enter a number: 85
85 is an odd number.
Nested IF Else Statement in C Programming
Output 2:
Please enter a number: 506
85 is an even number.
Nested IF Else Statement in C Programming
Output 3:
Please enter a number: 8
85 is a single digit number.
Nested IF Else Statement in C Programming
Explanation:
If you have understood the program and its outputs well, then you do not need to read the explanation given below.
In the above example, we have first asked the users for their age input, which we have held in a variable named x.
Now different users will input their different age i.e. 6, 8, 17, 26, 84 etc. and similar users will input their work experience in different years.
Nested IF Else Statement in C Programming
Let us understand nested if else conditional statements with all the three outputs one by one.
In Output 1, the user has an input value of x as 85. Now first of all the condition with age (x>9) will be checked in outer if and that condition will become true (85>9) according to user’s input.
As soon as the condition of the if is true, the flow of the program will go inside the if and then there will be a nested if i.e. the condition (x%2==0) with experience in the inner if and that condition will become false (1==0) according to the user’s input.
Due to the condition of the inner if being false, the flow of the program will go inside the else of the inner if and the printf statement will run.
Nested IF Else Statement in C Programming
In Output 2, the user has input x as 506. Now first of all the condition with age (x>9) will be checked in outer if and that condition will become true (506>9) according to user’s input.
As soon as the condition of the if is true, the flow of the program will go inside the if and then there will be a nested if i.e. the condition (x%2==0) in the inner if and that condition will become true(0==0) according to the user’s input.
Due to the condition of the inner if being true, the flow of the program will go inside the inner if and the printf statement will run.
In Output 3, the user has input x as 8. Now first of all the condition with age (x>9) will be checked in outer if and that condition will become false (8>9) according to user’s input.
Due to the if condition being false, the flow of the program will go to the else of the outer if and the printf statement will run.
Excercise
If you want to understand the nested if else conditional statement better, then definitely understand the programs given below.
Read:Â C Program to Find the Largest of Three Numbers using Nested if else statement
I hope that you have liked this post. If you liked the post, then please share it with your friends and relatives and you can send your suggestion in the comment box below or email me at pr***********@gm***.com.