EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Nested if Statement in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Nested if Statement in C

Nested if Statements in C

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:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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:

Nested if 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.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,617 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

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:

Nested if Statements in C Example 1

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:

Nested if Statements in C Example 2

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:

Greatest Digit Example 3

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:

 Greater or Equal Example 4

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 –

  1. Reverse String in C
  2. Nested if Statements in Java
  3. Nested if in JavaScript
  4. Function Prototype in C

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More