Nested if-else statement in C
If we use an if-else statement within another if statement in a C program, it is called a nested if-else statement in C. It helps to check the condition inside another condition.
Purpose of using Nested if else Statement:
We can check multiple conditions and return different values, all in a single form, depending on the results of those checks.
Example Statement:
All children under or equal the age of 12 are entitled to free tuition. However, the organization does not offer free classes to all children. Therefore, we use another if statement (also known as a nested if statement in C) to see their family's income. If annual income of their family is below or equal to 100000 (1 lakh), they will get free tuition.
Syntax:
if (req 1) //if the condition is true , this will execute
{
if (req 2) //if the condition is true ,it will execute and print
{
printf("___");
}
else //if req 2 condition is not true then only else part will execute and print
{
printf("___");
}
else //if req 1 condition is false then this else part will execute.
{
printf("____");
}
Example of if else statement:
This example uses only if-else conditions. If students have a score of 75% or higher here, they are eligible for a scholarship, otherwise they are not.
Code:
#include <stdio.h>
int main()
{
int score;
printf("Enter your score out of 100:");
scanf("%d",&score);
if(score >=75) //if the condition is true , this will execute
{
printf("You are eligible for scholarship");
}
else //if the 'if' condition is false then this else part will execute.
{
printf("You are not eligible for scholarship");
}
return 0;
}
Output:
Input: 96
Output:
You are eligible for scholarship
Examples of Nested if else Statement:
Example 1:
All children under or equal the age of 12 are entitled to free lessons. However, this organization does not offer free classes to all children. If their family's annual income is less than or equal to 1 lakh, you will receive a free tuition.
Code:
#include <stdio.h>
int main()
{
int age, income; //considering two variables
printf("Enter your age: ");
scanf("%d",&age);
if(age <=12) //if the condition is true , this will execute
{
printf("\nEnter your family income: ");
scanf("%d",&income);
if(income<=100000) // it executes if their annual income is below or more than 1 lakh
printf("You are eligible for free tuition");
else //more than annual 1lakh income is not eligible for scholarship
printf("You are not eligible for free tuition");
}
else //if the 'if' condition is false then this else part will execute.
{
printf("You are not eligible for free tuition");
}
return 0;
}
Output 1:
Input 1:
25
Output:
You are not eligible for free tuition
Output 2:
Input 2:
11
96000
Output :
You are eligible for free tuition
Output 3:
Input 3:
9
1200000
Output :
You are not eligible for free tuition
Example 2:
All students under or equal the age of 25 are eligible to enrol in a prestigious university. However, the university does not accept all students. If a student has a percentage of the latest board exams of 75 or above, you have a chance to enrol.
Code:
#include <stdio.h>
int main()
{
int age,score; //considering two variables
printf("Enter your age: ");
scanf("%d",&age);
if(age <= 25) //if the condition is true , this will execute
{
printf("\nEnter your percentage of last board exam : ");
scanf("%d",&score);
if(score<=100 && score>=75) // it executes if a student has more than or equal to 75% score
printf("You are eligible to get admission in our College");
else //less than 75% score is not qualified to get admission
printf("You are not qualified to get admission in our College");
}
else //if the 'if' condition is false then this else part will execute.
{
printf("You are not qualified to get admission in our College");
}
return 0;
}
Output 1:
Input 1:
22
76
Output :
You are eligible to get admission in our College
Output 2:
Input 2:
31
Output:
You are not qualified to get admission in our College
Output 3:
Input 3:
20
60
Output:
You are not qualified to get admission in our College