C# nested if else if else statement


A nested if-else if-else statement in C# occurs when an if-else if-else statement is placed inside another if or else block. This allows you to evaluate more complex conditions and execute different blocks of code based on multiple criteria.

1. What is a Nested if-else if-else Statement?

In a nested if-else structure, an if-else block is contained within another if or else block. This is useful when you need to make further decisions based on the result of a previous condition.

2. Syntax of Nested if-else if-else

if (condition1) { // Code block for condition1 being true if (condition2) { // Code block for condition2 being true (when condition1 is true) } else { // Code block for condition2 being false (when condition1 is true) } } else if (condition3) { // Code block for condition3 being true (when condition1 is false) } else { // Code block when all conditions are false }

3. Example of a Nested if-else if-else Statement

Let's look at an example where we classify a number based on two conditions: whether it is positive or negative, and whether it is even or odd.

int number = 15; if (number > 0) { Console.WriteLine("The number is positive."); if (number % 2 == 0) { Console.WriteLine("The number is even."); } else { Console.WriteLine("The number is odd."); } } else if (number < 0) { Console.WriteLine("The number is negative."); if (number % 2 == 0) { Console.WriteLine("The number is even."); } else { Console.WriteLine("The number is odd."); } } else { Console.WriteLine("The number is zero."); }

Explanation:

  • The outer if-else checks whether the number is positive, negative, or zero.
  • If the number is positive or negative, the inner if-else block further checks whether it is even or odd.
  • If the number is zero, the else block runs, printing "The number is zero.".

4. Flow of Execution in Nested if-else if-else

  1. The outer if condition is evaluated first.
    • If true, the code block inside the outer if is executed.
    • If false, the next else if or else block is evaluated.
  2. Inside the true branch of the outer if, the inner if-else statement is evaluated next.
    • If the condition inside the inner if is true, its block executes.
    • If false, the inner else block executes.
  3. The process continues with each nested condition.

5. Example: Classifying Age Groups

This example shows a nested if-else statement where we classify a person based on their age and whether they are a student.

int age = 22; bool isStudent = true; if (age >= 18) { Console.WriteLine("You are an adult."); if (isStudent) { Console.WriteLine("You are a student."); } else { Console.WriteLine("You are not a student."); } } else { Console.WriteLine("You are a minor."); if (isStudent) { Console.WriteLine("You are a student."); } else { Console.WriteLine("You are not a student."); } }

Explanation:

  • The outer if checks if the person is an adult (age 18 or above).
    • If true, it prints "You are an adult." and checks if the person is a student using an inner if-else block.
    • If false (the person is a minor), the program executes the else block and checks again if they are a student or not.
  • This allows us to handle two conditions: age classification and student status.

6. Example with More Complex Logic

Consider a scenario where we determine eligibility for a discount based on both age and whether the person is a member.

int age = 60; bool isMember = true; if (age >= 60) { Console.WriteLine("Senior Citizen Discount Applies."); if (isMember) { Console.WriteLine("Additional membership discount applies."); } else { Console.WriteLine("No additional membership discount."); } } else if (age >= 18) { Console.WriteLine("Adult Pricing Applies."); if (isMember) { Console.WriteLine("Membership discount applies."); } else { Console.WriteLine("No membership discount."); } } else { Console.WriteLine("Child Pricing Applies."); }

Explanation:

  • If the person is a senior citizen (age 60 or older), they receive a senior citizen discount, and if they are a member, they get an additional membership discount.
  • If they are an adult (age 18 or older but under 60), they receive adult pricing, with a membership discount if applicable.
  • If they are under 18, child pricing applies.

7. Summary of Nested if-else if-else Statements

  • Nested if-else statements allow for decision-making within other decisions, making it useful when multiple layers of logic are required.
  • The inner if-else block is executed only if the outer if or else condition is true.
  • Nested conditions help handle more complex scenarios where multiple factors influence the outcome.
  • Be mindful of code readability when nesting multiple conditions. For very complex logic, consider alternative structures (like switch statements or separate methods).

Using nested if-else helps make decisions based on multiple conditions, but it's essential to ensure the logic remains clear and maintainable.