C# Nested If Else Statements with Examples

  Total Views : 116
  Comments : 0
  Keywords : C# nested if-else condition to execute multiple if…else conditions with examples, C# nested if…else statements to use one if or else…if statements inside of another if…else statement example

Here we will learn nested if else statements in c# with examples and how to use nested if-else conditions in c# to write if…else statement within another if…else statement to test one condition followed by another condition with examples.

C# Nested If-Else Statement

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# by placing one if…else statement within another if…else statement is called as 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 above c# nested if-else statement syntax, we defined a one if…else statement within another if…else condition to perform one condition followed by another condition.

 

In nested if-else statement, the defined if condition returns true then it will enter into 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 which 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 above c# nested if-else statements flow chart, if the defined condition is true, then another if…else condition execution will happen and perform 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 Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above code, we defined a 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 above c# program, we will get the result like as shown below.

 

C# Nested If Else Statement Example Result

 

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

 

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

 Comments (0)

Be the first to give your valuable feedback

  • Default Tutlane User