Skip to content

C# - Nested If

In C#, it is perfectly valid to place an if or if-else statement inside another if or else block. This is called nesting. Nested if statements are used to test for a secondary condition only if the primary condition is true.

The basic syntax for a nested if statement is as follows:

if (primary_condition)
{
// Code to execute if primary_condition is true
if (secondary_condition)
{
// Code to execute if both primary_condition and secondary_condition are true
}
}

You can also nest if-else and else if statements to handle more complex scenarios.

This example checks if a number is positive, and if so, it then checks if it is an even number.

int number = 10;
// Primary condition: Check if the number is positive
if (number > 0)
{
Console.WriteLine("The number is positive.");
// Nested condition: Check if the number is even
if (number % 2 == 0)
{
Console.WriteLine("And it is also an even number.");
}
else
{
Console.WriteLine("And it is an odd number.");
}
}
else
{
Console.WriteLine("The number is not positive.");
}
// Output:
// The number is positive.
// And it is also an even number.

Here, we use nested logic to provide more detailed feedback on a student’s grade.

int marks = 85;
if (marks >= 90)
{
Console.WriteLine("Grade: A+");
}
else if (marks >= 80)
{
Console.WriteLine("Grade: A");
// Nested check for high-A performance
if (marks >= 85)
{
Console.WriteLine("Excellent Performance!");
}
else
{
Console.WriteLine("Good Job!");
}
}
else if (marks >= 70)
{
Console.WriteLine("Grade: B");
}
else
{
Console.WriteLine("Grade: F - Needs Improvement");
}
// Output:
// Grade: A
// Excellent Performance!

While nested if statements are powerful, they can quickly become hard to read and maintain if they go too deep. This is often called the “Arrow Anti-pattern” because the code begins to look like an arrowhead ->.

A guard clause is a check at the beginning of a method that exits early if a condition isn’t met. This reduces nesting and keeps the main logic at the top level.

// --- BEFORE: Nested ---
public void ProcessOrder(Order order)
{
if (order != null)
{
if (order.IsVerified)
{
if (!order.IsShipped)
{
// Main logic here...
Console.WriteLine("Processing the order...");
}
}
}
}
// --- AFTER: Using Guard Clauses ---
public void ProcessOrder(Order order)
{
if (order == null) return;
if (!order.IsVerified) return;
if (order.IsShipped) return;
// Main logic is now clean and un-nested
Console.WriteLine("Processing the order...");
}

2. Combine Conditions with Logical Operators

Section titled “2. Combine Conditions with Logical Operators”

If the logic allows, you can combine conditions using && (AND) to flatten the structure.

// Instead of nesting...
if (user.IsAuthenticated)
{
if (user.HasPermissions)
{
// ...
}
}
// Combine the conditions
if (user.IsAuthenticated && user.HasPermissions)
{
// ...
}

For complex conditional logic based on multiple states, a switch expression can be more readable than a deeply nested if-else chain.

// Using a tuple to check multiple conditions at once
string GetWeatherDescription(bool isRaining, int temperature)
{
return (isRaining, temperature) switch
{
(true, < 0) => "Freezing rain. Stay inside!",
(true, _) => "It's raining.",
(false, > 30) => "It's hot and sunny.",
(false, < 10) => "It's cold but dry.",
_ => "It's a pleasant day."
};
}
Console.WriteLine(GetWeatherDescription(true, -5)); // Output: Freezing rain. Stay inside!
Console.WriteLine(GetWeatherDescription(false, 35)); // Output: It's hot and sunny.