Uploaded by Narong Mettripun

Top 4 Examples of Nested if Statement in C

advertisement
🚀
Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ $199 90% 
OFF Ends in 0 0 D 0 1 H 5 6 M 1 5 S
(https://www.educba.
com/)
| BLOG (https://www.educba.com/blog/)
Nested if Statement in C
Article by
blog@educba.com (mailto:blog@educba.com)
Anusua Dutta (https://www.educba.com/author/anusua-dutta/)
Reviewed by
Free Software D
SkillUP in Python Programm
Priya Pedamkar (https://www.educba.com/author/priya-pedamkar/)
 (https://www.educba.com/else-if-statementin-c/)
*Please provide your correct email id. Login
 (https://www.educba.com/hash-else-inc/)
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
(https://vue.comm100.com/chatwindow.aspx?siteId=90004140&planId=88106100-2271-458b-b0c7-1d3108bb141c)
if statement with an else very carefully.
🚀
y
y
Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ $199 90% 
OFF Ends in 0 0 D 0 1 H 5 6 M 1 5 S
Start Your Free Software Development Course
| BLOG (https://www.educba.com/blog/)
(https://www.educba.
Web development, programming languages, Software testing & others
com/)
blog@educba.com (mailto:blog@educba.com)
Syntax:
Free Software D
if ( check 1st condition)
{
if ( check 2nd condition)
SkillUP in Python Programm
*Please provide your correct email id. Login
{
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:
(https://vue.comm100.com/chatwindow.aspx?siteId=90004140&planId=88106100-2271-458b-b0c7-1d3108bb141c)
🚀
Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ $199 90% 
OFF Ends in 0 0 D 0 1 H 5 6 M 1 5 S
(https://www.educba.
com/)
| BLOG (https://www.educba.com/blog/)
blog@educba.com (mailto:blog@educba.com)
Free Software D
SkillUP in Python Programm
*Please provide your correct email id. Login
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
Learning Paths @ $19
ADVERTISEMENT
Most Popular Learning Paths in Finance, Financial Modeling and
Excel just for $19
5 to 30+ Courses | 20 to 100+ Hours of Videos | Certificates for each Course Completed
(https://www.educba.com/learning-paths-offer/?coursename=finance)
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
(https://vue.comm100.com/chatwindow.aspx?siteId=90004140&planId=88106100-2271-458b-b0c7-1d3108bb141c)
become true and satisfactory for the other statement with the second condition then there
🚀
y
Extended
Price One
Drop!very
All special
in One characteristic
Universal Bundle
(3700+ Courses)
@ of$199 90% 
will be
need of Cyber
NestedMonday
if statement.
to describe
such type
OFF Ends in 0 0 D 0 1 H 5 6 M 1 5 S
uncertain logic behind this is helpful with Nested If statement.
| BLOG (https://www.educba.com/blog/)
(https://www.educba.
Controlcom/)
statement like if can be easily nested within anotherblog@educba.com
nested if statement
besides
(mailto:blog@educba.com)
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
Free Software D
SkillUP in Python Programm
*Please provide your correct email id. Login
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()
{
(https://vue.comm100.com/chatwindow.aspx?siteId=90004140&planId=88106100-2271-458b-b0c7-1d3108bb141c)
int a;
🚀
int a;
printf("
Enter
your
currentPrice
Age Drop!
Here:\n");
Extended
Cyber
Monday
All in One Universal Bundle (3700+ Courses) @ $199
scanf("%d",&a);
OFF Ends in 0 0 D 0 1 H 5 6 M 1 5 S
if ( a < 18 )
{
(https://www.educba.
printf("Consider as minor \n");
com/)
printf("Not
fit for Working");
90% 
| BLOG (https://www.educba.com/blog/)
blog@educba.com (mailto:blog@educba.com)
}
else
Free Software D
{
if (a >= 18 && a <= 50 )
{
printf("He/She is successfully eligible for Working \n");
SkillUP in Python Programm
*Please provide your correct email id. Login
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:
Learning Paths @ $19
ADVERTISEMENT
Most Popular Learning Paths in Web Dev, Programming, Cyber
Security and Testing just for $19
5 to 30+ Courses | 20 to 100+ Hours of Videos | Certificates for each Course Completed
(https://www.educba.com/learning-paths-offer/?coursename=software-development)
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:
(https://vue.comm100.com/chatwindow.aspx?siteId=90004140&planId=88106100-2271-458b-b0c7-1d3108bb141c)
#include <stdio.h>
🚀
int main()
Extended
{
Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ $199 90% 
OFF Ends in 0 0 D 0 1 H 5 6 M 1 5 S
int x = 65, y = 35, z = 2;
if (x > y)
{
(https://www.educba.
if (x
> z)
com/)
| BLOG (https://www.educba.com/blog/)
{
printf("x is larger than y and z ");
}
}
printf("\n flow for the program is proper ");
return 0;
}
blog@educba.com (mailto:blog@educba.com)
Free Software D
SkillUP in Python Programm
*Please provide your correct email id. Login
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()
{
(https://vue.comm100.com/chatwindow.aspx?siteId=90004140&planId=88106100-2271-458b-b0c7-1d3108bb141c)
int dig1, dig2, dig3;
🚀
int dig1, dig2, dig3;
printf("Enter
three
numbers:
");Drop! All in One Universal Bundle (3700+ Courses) @ $199
Extended Cyber
Monday
Price
scanf("%d%d%d", &dig1, &dig2,OFF
&dig3);
Ends in 0 0 D 0 1 H 5 6 M 1 5 S
if(dig1 > dig2)
{
(https://www.educba.
if(dig1 > dig3)
com/)
{
90% 
| BLOG (https://www.educba.com/blog/)
blog@educba.com (mailto:blog@educba.com)
printf("dig1 is the maximum");
Free Software D
}
else
{
printf("dig3 is the maximum");
}
SkillUP in Python Programm
*Please provide your correct email id. Login
}
else
{
if(dig2 > dig3)
{
printf("dig2 is the maximum");
}
else
{
printf("dig3 is the maximum");
}
}
return 0;
}
Output:
Learning Paths @ $19
ADVERTISEMENT
Most Popular Learning Paths in Data Science, Machine Learning
and AI just for $19
5 to 30+ Courses | 20 to 100+ Hours of Videos | Certificates for each Course Completed
(https://www.educba.com/learning-paths-offer/?coursename=data-science)
Example #4
Program to take certain numbers as input from the user and then calculating from those
(https://vue.comm100.com/chatwindow.aspx?siteId=90004140&planId=88106100-2271-458b-b0c7-1d3108bb141c)
numbers the largest and then giving the result whether or not it is greater or equal after
🚀
u be s t e a gest a d t e g
g t e esu t
et e o ot t s g eate o equa a te
Extendedwith
Cyber
Monday
Price Drop! All in One Universal Bundle (3700+ Courses) @ $199 90% 
manipulation
nested
if statement.
OFF Ends in 0 0 D 0 1 H 5 6 M 1 5 S
Code:
(https://www.educba.
com/)
| BLOG (https://www.educba.com/blog/)
blog@educba.com (mailto:blog@educba.com)
#include <stdio.h>
int main()
{
Free Software D
SkillUP in Python Programm
int g1, g2;
printf("Get value for g1:");
scanf("%d", &g1);
printf("Get value for g2:");
*Please provide your correct email id. Login
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.
(https://vue.comm100.com/chatwindow.aspx?siteId=90004140&planId=88106100-2271-458b-b0c7-1d3108bb141c)
Conclusion
🚀
Allifinstatement
One Universal
Bundleis(3700+
Courses)
Extendedcan
Cyber
Monday
Price
A conclusion
be easily
made
thatDrop!
nesting
to perform
fine but
when it@ $199 90% 
OFF Ends in 0 0 D 0 1 H 5 6 M 1 5 S
comes to deal with the false statement once it enters the else part and control needs to be
(https://www.educba.com/blog/)
executed
and set to| aBLOG
true value
then nested if it comes as a savior.
(https://www.educba.
com/)
Recommended Articles
blog@educba.com (mailto:blog@educba.com)
Free Software D
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 –
SkillUP in Python Programm
*Please provide your correct email id. Login
1. Reverse String in C (https://www.educba.com/reverse-string-in-c/)
2. Nested if Statements in Java (https://www.educba.com/nested-if-statements-injava/)
3. Nested if in JavaScript (https://www.educba.com/nested-if-in-javascript/)
4. Function Prototype in C (https://www.educba.com/function-prototype-in-c/)
ADVERTISEMENT
All in One Excel VBA
Bundle
 500+ Hours of HD Videos
 15 Learning Paths
 120+ Courses
 Verifiable Certificate of Completion
 Lifetime Access

(https://www.educba.com/excel/cours
es/excel-vba-certification-course/?
btnz=edu-after-post-banner)
ADVERTISEMENT
Financial Analyst Masters
Training Program
 2000+ Hours of HD Videos
 43 Learning Paths
 550+ Courses
 Verifiable Certificate of Completion
 Lifetime Access

(https://vue.comm100.com/chatwindow.aspx?siteId=90004140&planId=88106100-2271-458b-b0c7-1d3108bb141c)
(https://www.educba.com/finance/cou
/
/
🚀
rses/financial-analyst-course/?
Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ $199 90% 
btnz=edu-after-post-banner)
OFF Ends in 0 0 D 0 1 H 5 6 M 1 5 S
(https://www.educba.
com/)
| BLOG (https://www.educba.com/blog/)
ADVERTISEMENT
All in One Data Science
Bundle
 2000+ Hour of HD Videos
 80 Learning Paths
blog@educba.com (mailto:blog@educba.com)
Free Software D
SkillUP in Python Programm
 400+ Courses
 Verifiable Certificate of Completion
 Lifetime Access
*Please provide your correct email id. Login

(https://www.educba.com/datascience/courses/data-science-course/?
btnz=edu-after-post-banner)
ADVERTISEMENT
All in One Software
Development Bundle
 5000+ Hours of HD Videos
 149 Learning Paths
 1050+ Courses
 Verifiable Certificate of Completion
 Lifetime Access

(https://www.educba.com/softwaredevelopment/courses/softwaredevelopment-course/?btnz=edu-afterpost-banner)
(https://vue.comm100.com/chatwindow.aspx?siteId=90004140&planId=88106100-2271-458b-b0c7-1d3108bb141c)
🚀
Follow us!
Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ $199 90% 
OFF Ends in 0 0 D 0 1 H 5 6 M 1 5 S
| BLOG (https://www.educba.com/blog/)
(https://www.facebook.com/educba.official)(https://twitter.com/EducbaBusiness)
(https://www.educba.
com/)
blog@educba.com (mailto:blog@educba.com)
(https://in.linkedin.com/company/corporate-bridge-academy)
(https://www.instagram.com/educba.official/)
(https://www.youtube.com/channel/UC1_jSu02VOW1pusqb2NF49g)
Free Software D
SkillUP in Python Programm
*Please provide your correct email id. Login
(https://www.coursera.org/educba)(https://www.udemy.com/user/premananda-suna/)
APPS
(https://play.g
id=com.educb
(https://apps.a
learning-app/i
Blog
Blog (https://www.educba.com/blog/?source=footer)
Free Tutorials
(https://www.educba.com/tutorials/?source=footer)
(https://vue.comm100.com/chatwindow.aspx?siteId=90004140&planId=88106100-2271-458b-b0c7-1d3108bb141c)
🚀
About us (https://www.educba.com/about-us/?source=footer)
Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ $199 90% 
Contact us (https://www.educba.com/contact-us/?source=footer)
OFF Ends in 0 0 D 0 1 H 5 6 M 1 5 S
Blog as Guest (https://www.educba.com/blog-as-guest/?source=footer)
(https://www.educba.
com/)
Courses
| BLOG (https://www.educba.com/blog/)
blog@educba.com (mailto:blog@educba.com)
Free Courses (https://www.educba.com/skillup-101free/?source=footer)
Explore Programs (https://www.educba.com/explore/?source=footer)
All Courses (https://www.educba.com/courses/?source=footer)
Free Software D
SkillUP in Python Programm
*Please provide your correct email id. Login
All in One Bundles (https://www.educba.com/all-in-one-bundles/?source=footer)
Sign up (https://www.educba.com/sign-up/?source=footer)
Email
blog@educba.com (mailto:blog@educba.com)
ISO 10004:2018 & ISO 9001:2015 Certified
© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE
OWNERS.
(https://vue.comm100.com/chatwindow.aspx?siteId=90004140&planId=88106100-2271-458b-b0c7-1d3108bb141c)
Download