C# if-else Statement

When if statement becomes true the if code block gets executed, and when it becomes false the control is transferred to the next statement after the if block. Sometimes we need to run some code when if statement becomes false, for this else statement used.

The else statement is an optional statement which is used with if statement. The else statement code block gets executed when the if statement becomes false.

Following is the Syntax of an if-else statement.

if (condition)
{
    statement1
}
else
{
    statement2
}

Both if or else statements can have one or more lines of code. For a single line of code, the braces are optional, but it's good practice to use braces for a single line as well.

The braces are required for more than one line of code.

Let's understand the if-else flow with an image.

Example

Try It
using System;
 
namespace TutorialsLeader
{
    public class MyFirstClass
    {
        static void Main()
        {
            int a = 1, b = 20;
 
            if (a > b)
            {
                Console.WriteLine("a is greater than b");
            }
            else
            {
                Console.WriteLine("a is less than b");
            }
        }
    }
}

Inside the if or else block there can be any kind of statement. It may contain another nested if or nested if-else or it may have a simple line of code.

Let's understand this with an example.

Example

Try It
using System;
 
namespace TutorialsLeader
{
    public class MyFirstClass
    {
        static void Main()
        {
            int a = 50, b = 20, c = 30;
 
            if (a > b)
            {
                if (a > c)
                {
                    Console.WriteLine("a is greater than b and c");
                }
                else
                {
                    Console.WriteLine("a is greater than b but less than c");
                }
            }
            else
            {
                if (a > c)
                {
                    Console.WriteLine("a is less than b but greater than c");
                }
                else
                {
                    Console.WriteLine("a is less than b and c");
                }
            }
        }
    }
}

if-else ladder Statements

The if-else-if-else ladder statement works similar to switch statement. It executes one condition from multiple statements which match.

Example

Try It
using System;
 
namespace TutorialsLeader
{
    public class MyFirstClass
    {
        static void Main()
        {
            int a = 1;
 
            if (a < 0)
            {
                Console.WriteLine("The value of a is a negative number");
            }
            else if (a == 0)
            {
                Console.WriteLine("The value of a is zero");
            }
            if (a == 1)
            {
                Console.WriteLine("The value of a is one");
            }
            else if (a == 2)
            {
                Console.WriteLine("The value of a is two");
            }
            else if (a == 3)
            {
                Console.WriteLine("The value of a is three");
            }
            else if (a == 4)
            {
                Console.WriteLine("The value of a is four");
            }
            else if (a == 5)
            {
                Console.WriteLine("The value of a is five");
            }
            else
            {
                Console.WriteLine("The value of a is greater than five");
            }
        }
    }
}

Let's learn about the switch statement in the next section.