Top 4 Examples of Nested if Statement - C Programming

Top 4 Examples of Nested if Statement - C Programming
Techiio-author
Written by Shuvhojit DebFebruary 12, 2022
7 min read
C
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

In this blog, you'll learn about the nested if statement in C.

About Nested if Statement:

Nested If in C Programming is placing If Statement inside another IF Statement. Nested If in C is helpful if you want to check the condition inside a condition. If Else Statement prints different statements based on the expression result (TRUE, FALSE). Sometimes we have to check even further when the condition is TRUE. In these situations, we can use these C Nested IF statements, but be careful while using them.

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 are like if statement will check for the first condition then if it gets satisfied with a true value 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 does not satisfy to be true it will go to the else section to verify for the second condition of false statement. And final nested if or else to check for the true condition.

blogpost

Examples of Nested if Statement in C:

1) Example

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

blogpost

2) Example

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

blogpost

3) Example

Program to find the greatest digit from three digits by making certain permutation and combination with nested if and then getting 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:-

blogpost

4) Example

Program to take certain numbers as input from the user and then calculate from those numbers the largest and then give 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:-

blogpost

Conclusion:

A conclusion can be fluently made that nesting if statement to perform is fine but when it comes to dealing with the false statement once it enters the additional part and control needs to be executed and set to a true value also nested if it comes as a rescuer.

C Language
Programming
Nested if
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance