Updated March 27, 2023
Introduction to Nested if Statement in C
Nested if statement in C is the nesting of if statement within another if statement and nesting of if statement with an else statement. Once an else statement gets failed there are times when the next execution of statement wants to return a true statement, there we need nesting of if statement to make the entire flow of code in a semantic order. Nested if statement in C plays a very pivotal role in making a check on the inner nested statement of if statement with an else very carefully.
Syntax:
if ( check 1st condition)
{
if ( check 2nd condition)
{
Verify True statements of 2nd condition;
}
else
{
Verify False statements of 2nd condition;
}
else
{
Verify False statements of 1st condition;
}
Explanation:
How the flow of the syntax of the nested if statement works is like if statement will check for the first condition then if it gets satisfied with a true value then it will check for the 2nd condition. Again, if the 2nd condition gets satisfied and the value comes out to be true that set of the statement will get executed. In case it do not satisfies to be true it will go to else section to verify for the second condition of false statement. And final nested if or else to check for the true condition.
Flowchart:
The flow of execution goes in a way that condition 1 will get tested if it becomes false then, statement 3 will get executed. If the condition 1 gets satisfied i.e. if it gets true then it will go for the next execution of test condition 2. In case the statement with condition 2 gets false or unsatisfied then it will execute else with statement 2 in consideration.
Working of Nested if Statement in C
An Example will be good to illustrate the working concept of Nested if statement. Let’s take an example and understand. Every person is eligible for working once he or she is above 18 years otherwise not eligible. Moreover, any organization will offer a job if he or she is above 18 years otherwise no job is guaranteed it means the condition then and there becomes false. Therefore, we will make use of another nested if statement to check the required qualification or any other special skill or requirement gets satisfied with this.
This working of nested if the statement is done in a way that when an if the condition gets true and other statements can go for a false condition but then it presumes that it has to become true and satisfactory for the other statement with the second condition then there will be need of Nested if statement. One very special characteristic to describe such type of uncertain logic behind this is helpful with Nested If statement.
Control statement like if can be easily nested within another nested if statement besides the fact that if outer statement gets failed then the compiler will skip the entire block irrespective of any other inner statement condition.
Examples of Nested if Statement in C
Below are the example of Nested if Statement in C:
Example #1
Program for analysis of people of certain age groups who are eligible for getting a suitable job if their condition and norms get satisfied using nested if statement.
Code:
#include <stdio.h>
int main()
{
int a;
printf(" Enter your current Age Here:\n");
scanf("%d",&a);
if ( a < 18 )
{
printf("Consider as minor \n");
printf("Not fit for Working");
}
else
{
if (a >= 18 && a <= 50 )
{
printf("He/She is successfully eligible for Working \n");
printf("Fill all the details and apply for it\n");
}
else
{
printf("Age is not satisfactory according to the organization norms\n");
printf("Ready for retirement and can collect pension \n");
}
}
return 0;
}
Output:
Example #2
Program to find which number is greater among the considered number and then how the execution happens with the help of nested if statement if the flow gets successful then it is counted as normal flow.
Code:
#include <stdio.h>
int main()
{
int x = 65, y = 35, z = 2;
if (x > y)
{
if (x > z)
{
printf("x is larger than y and z ");
}
}
printf("\n flow for the program is proper ");
return 0;
}
Output:
Example #3
Program to find the greatest digit from three digits by making certain permutation and combination with nested if and then getting an output with the three largest among all.
Code:
#include <stdio.h>
int main()
{
int dig1, dig2, dig3;
printf("Enter three numbers: ");
scanf("%d%d%d", &dig1, &dig2, &dig3);
if(dig1 > dig2)
{
if(dig1 > dig3)
{
printf("dig1 is the maximum");
}
else
{
printf("dig3 is the maximum");
}
}
else
{
if(dig2 > dig3)
{
printf("dig2 is the maximum");
}
else
{
printf("dig3 is the maximum");
}
}
return 0;
}
Output:
Example #4
Program to take certain numbers as input from the user and then calculating from those numbers the largest and then giving the result whether or not it is greater or equal after manipulation with nested if statement.
Code:
#include <stdio.h>
int main()
{
int g1, g2;
printf("Get value for g1:");
scanf("%d", &g1);
printf("Get value for g2:");
scanf("%d",&g2);
if (g1 != g2)
{
printf("g1 is not equal to g2\n");
if (g1 > g2)
{
printf("g1 is larger than g2\n");
}
else
{
printf("g2 is larger than g1\n");
}
}
else
{
printf("g1 is equal to g2\n");
}
return 0;
}
Output:
With the above-illustrated programs, it can be very well analyzed that nested if statement plays a very critical role when it comes to condition satisfaction with the scenarios involving all the critical decision-making statements with assertions and manipulations being involved.
Conclusion
A conclusion can be easily made that nesting if statement to perform is fine but when it comes to deal with the false statement once it enters the else part and control needs to be executed and set to a true value then nested if it comes as a savior.
Recommended Articles
This is a guide to Nested if Statement in C. Here we discuss the Introduction to Nested if Statement in C and its Examples along with its Code Implementation. You can also go through our other suggested articles to learn more –