C# Nested If Else Statements with Examples

In c#, Nested if-else statements or conditions are useful to include one if…else statement within another if…else statement to test one condition followed by another condition.

 

Generally, in c# placing one if…else statement within another if…else statement is called a nested if…else statement.

Syntax of C# Nested If-Else Statement

Following is the syntax of defining nested if…else statement in c# programming language.

 

if (condition)
{
if (nested_condition_1)
{
// Statements to Execute
}
else
{
// Statements to Execute
}
}
else
{
if (nested_condition_2)
{
// Statements to Execute
}
else
{
// Statements to Execute
}
}

If you observe the above c# nested if-else statement syntax, we defined one if…else statement within another if…else condition to perform one condition followed by another condition.

 

In a nested if-else statement, the defined if condition returns true, it will enter into the body of the condition and perform another if…else condition checking based on our requirements.

C# Nested If-Else Statement Flow Chart Diagram

Following is the flow chart diagram that will represent the process flow of nested-if-else statement in c# programming language.

 

C# Nested If Else Statements Flow Chart Diagram

 

If you observe the above c# nested if-else statements flow chart if the defined condition is true, then another if…else condition execution will happen and perform the required operations. Same way, other nested if-else statements also will be executed based on our requirements.

C# Nested If-Else Statement Example

Following is the example of defining a nested if-else statement in c# programming language to execute the block of code or statements followed by another block of code based on our requirements.

 

using System;

namespace Tutlane
{
    class Program
    {
        static void Main(string[] args)
        {
             int x = 5, y = 20;
             if (x > y)
             {
                 if (x >= 10)
                 {
                     Console.WriteLine("x value greater than or equal to 10");
                 }
                 else
                 {
                     Console.WriteLine("x value less than 10");
                 }
             }
             else
             {
                 if (y <= 20)
                 {
                     Console.WriteLine("y value less than or equal to 20");
                 }
                 else
                 {
                     Console.WriteLine("y value greater than 20");
                 }
             }
             Console.WriteLine("Press Enter Key to Exit..");
             Console.ReadLine();
        }
    }
}

If you observe the above code, we defined nested if-else statements to execute one condition followed by another condition based on our requirements.

Output of C# Nested If Else Statement Example

When we execute the above c# program, we will get the result below.

 

C# Nested If Else Statement Example Result

 

If you observe the above result, the nested if-else statements have been executed based on the conditions we defined and printed the required statement in the console window.

 

This is how we can use nested if-else statements in the c# programming language to execute the block of code or statements followed by another block of code or statements based on our requirements.