Nested if else Statement in C#



Nested if else Statement in C# – Nested if else statement in C# means, if else statement inside another if else statement.

Nested if else statement syntax in C# :

if(condition1)
{
if(condition2)
{
// execute code when condition1 and condition2 are true
}
else if(condition3)
{
if(condition4)
{
// execute code when condition3 and condition4 are true
}
else
{
// execute code when condition3 is true
// execute code when condition4 is false
}
}
}
else
{
// execute code when conditions1 is false
}

Example  of Nested if else statement in C#

int a = 15;
int b = 23;
int c = 10;

if (a > b)
{
  if (a > c)
   {
     Response.Write("a is greater than b and c");
   }
  else
   {
     Response.Write("c is greater than a and b");
   }
}
else
{
  if (b > c)
   {
     Response.Write("b is greater than a and c");
   }
  else
   {
     Response.Write("c is greater than a and b");
   }
}

The output is :
b is greater than a and c

Example of Nested if else statement in C#

int a = 15;
int b = 25;

if (a != b)
{
   if (a < b)
    {
     Response.Write("a is less than b");
    }
   else if (a > b)
    {
     Response.Write("a is greater than b");
    }
}
else
{
Response.Write("a is equal to b");
}

The output is :
a is less than b

Related ASP.Net Topics :

If Conditional Statement in C#.Net
If-Else If-Else Statement in C#.Net


Subscribe us

If you liked this c#.net post, then please subscribe to our YouTube Channel for more c#.net video tutorials.

We hope that this asp.net c# tutorial helped you to understand Nested If-Else Statement.


Next asp.net tutorial we will learn about Switch Statement.


Leave a Reply

Your email address will not be published. Required fields are marked *