Nested If Else Statement in C | About, Syntax, Flowchart and Examples

Nested If Else Statement in C | About, Syntax, Flowchart and Examples

6 mins readComment
Esha
Esha Gupta
Associate Senior Executive
Updated on Oct 13, 2024 21:23 IST

Have you ever wondered how programs make decisions and adapt to different situations? The answer lies in the power of Nested If else statements. These statements allow us to control the flow of our code based on specific conditions, enabling us to write flexible and responsive programs.

In C programming, a nested if-else statement is when you have an if-else block inside another if or else block. It's like placing one decision within another decision. You use this when you want to check for a new condition after a previous condition has already been found true (or false).

Let us see how the Nested If else statement works, starting with syntax, flowchart, and explanation.

Must read Control Statements in C

Syntax of Nested If else Statement

if (condition1) {

    // Statements to execute if condition1 is true

Recommended online courses

Best-suited C / C++ courses for you

Learn C / C++ with these high-rated online courses

4 K
2 months
– / –
6 months
– / –
1 month
3.5 K
3 months
15 K
2 months
– / –
50 hours
– / –
40 hours
– / –
2 months
– / –
4 months
5.5 K
140 hours

 

    if (condition2) {

        // Statements to execute if condition1 and condition2 are both true

    } else {

        // Statements to execute if condition1 is true and condition2 is false

    }

 

} else {

    // Statements to execute if condition1 is false

}

Flowchart of Nested If else Statement

Explanation of How Nested If-else Works

  1. The outer if-else statement's condition is evaluated first.
  2. If this condition is true, the code block within the if statement is executed, which may contain further nested if-else statements.
  3. If the outer condition is false, the code block within the outer else is executed, which could also contain nested if-else statements.
  4. Each nested if-else statement works the same way: if the condition is true, its if block is executed.If false, its else block is executed.
  5. This nesting can occur at multiple levels deep, with each level having its own if-else logic.
  6. The execution is not necessarily sequential from one if-else to another. It depends on the branching caused by the true or false evaluations at each level.

Examples of Nested If else statement in C

Let’s see some Nested If-else programs one by one.

Example 1: Grading System

Problem Statement: Determine the grade of a student based on their score. If the score is above 90, it's an 'A'. If the score is between 80 and 90, check if it's above 85 for an 'A-' or else 'B+'. Below 80, if it's above 70, it's a 'C', otherwise, it's a 'D'.


 
#include <stdio.h>
int main() {
int score = 88;
// Check if score is greater than 90
if (score > 90) {
printf("Grade: A
");
} else {
// Score is 90 or below, check if score is greater than 80
if (score > 80) {
// Score is between 81 and 90, check if score is above 85
if (score > 85) {
printf("Grade: A-
");
} else {
printf("Grade: B+
");
}
} else {
// Score is 80 or below, check if score is above 70
if (score > 70) {
printf("Grade: C
");
} else {
printf("Grade: D
");
}
}
}
return 0;
}
Copy code

Output

Grade: A-
Since the score is 88, the first if condition score > 90 is false, so it goes to the first else block. Inside, the condition score > 80 is true, so it checks the nested if condition score > 85, which is also true. Therefore, it prints "Grade: A-".

Example 2: Age Category and Voting Eligibility

Problem Statement: Classify a person's age group and determine if they are eligible to vote. A person under 18 is a minor, between 18 and 65 is an adult, and above 65 is a senior. Only adults and seniors are eligible to vote.


 
#include <stdio.h>
int main() {
int age = 20;
// Check if age is less than 18
if (age < 18) {
printf("Category: Minor
");
printf("Not eligible to vote.
");
} else {
// Age is 18 or above, check if age is 65 or below
if (age < = 65) {
printf("Category: Adult
");
printf("Eligible to vote.
");
} else {
// Age is above 65
printf("Category: Senior
");
printf("Eligible to vote.
");
}
}
return 0;
}
Copy code

Output

Category: Adult

Eligible to vote.

The age is 20, so the first if condition age < 18 is false. It moves to the else block, where age <= 65 is true. Therefore, it prints "Category: Adult" and "Eligible to vote."

Control Statements in C | Meaning and Types

Learning If Statement in C

Learning Loops in C

Explore Online C Programming Courses

Example 3: Discount Based on Membership and Purchase Amount

Problem Statement: A store gives discounts based on membership status and the amount spent. Members spending over Rs.100 get a 10% discount, non-members spending over Rs.150 get a 5% discount, and all others get no discount.


 
#include <stdio.h>
int main() {
int isMember = 1; // 1 for member, 0 for non-member
float purchaseAmount = 120.0;
// Check if the customer is a member
if (isMember) {
// Member is true, check if purchase amount is over Rs.100
if (purchaseAmount > 100) {
printf("Discount: 10%%
");
} else {
// Purchase amount is Rs.100 or less for a member
printf("Discount: 0%%
");
}
} else {
// Customer is not a member, check if purchase amount is over Rs.150
if (purchaseAmount > 150) {
printf("Discount: 5%%
");
} else {
// Purchase amount is Rs.150 or less for a non-member
printf("Discount: 0%%
");
}
}
return 0;
}
Copy code

Output

Discount: 10%

Here, isMember is 1 (true), so the first if condition is met. Inside, purchaseAmount > 100 is also true, so it prints "Discount: 10%". If isMember were 0 and purchaseAmount was greater than Rs.150, it would print "Discount: 5%". Otherwise, it would print "Discount: 0%".

 

Key Takeaways

  • Nested if-else statements allow for structured and multi-level decision-making processes within a program.
  • Each nested if-else condition evaluates a Boolean expression, which must be either true or false to determine the flow of execution.
  • Properly structured nested if-else statements can be efficient in terms of execution, as they allow the program to skip unnecessary checks once a true condition is found.

FAQs

What is a Nested If-Else Statement in C?

A nested if-else statement in C refers to an if-else structure placed within another if or else block. This allows for more complex decision-making processes by enabling the evaluation of multiple conditions in a hierarchical manner.

When would you use a Nested If-Else Statement?

You would use a nested if-else statement when your decision-making process requires evaluating several conditions that are dependent on the outcomes of previous conditions. It's useful for creating multi-level checks within your program.

Can Nested If-Else Statements become difficult to read or maintain?

Yes, deeply nested if-else statements can become hard to read and maintain, especially as the depth increases. It's generally recommended to keep the nesting to a manageable level or use alternative structures like switch-case statements or logical operators to simplify complex conditions.

How does a Nested If-Else Statement affect program execution?

A nested if-else statement can affect program execution by introducing multiple paths of execution depending on the outcomes of the evaluated conditions. This can make the flow of the program more complex but also allows for more precise control over program behaviour.

Are there best practices for using Nested If-Else Statements in C?

Best practices for using nested if-else statements include:

  • Keeping the nesting depth minimal to enhance readability.
  • Using comments to explain the logic behind complex nested conditions.
  • Considering alternative structures like logical operators or switch-case statements for simplifying conditions.
  • Ensuring that all possible conditions are covered to avoid unexpected behaviors.
About the Author
author-image
Esha Gupta
Associate Senior Executive

Hello, world! I'm Esha Gupta, your go-to Technical Content Developer focusing on Java, Data Structures and Algorithms, and Front End Development. Alongside these specialities, I have a zest for immersing myself in v... Read Full Bio