C# - if, else if, else Statements

Updated on:

C# provides many decision-making statements that help the flow of the C# program based on certain logical conditions. Here, you will learn about if, else if, else, and nested if else statements to control the flow based on the conditions.

C# includes the following flavors of if statements:

  1. if statement
  2. else-if statement
  3. else statement

C# if Statement

The if statement contains a boolean condition followed by a single or multi-line code block to be executed. At runtime, if a boolean condition evaluates to true, then the code block will be executed, otherwise not.

Syntax:
if(condition)
{
    // code block to be executed when if condition evaluates to true
}
Example: if Statement
int i = 10, j = 20;

if (i < j)
{
    Console.WriteLine("i is less than j");
}        

if (i > j)
{
    Console.WriteLine("i is greater than j");
}
Output:
i is less than j

In the above example, a boolean condition in the first if statement i < j evaluates to true, so the C# compiler will execute the following code block. The second if statement's condition i > j evaluates to false, so the compiler will not execute its code block.

The conditional expression must return a boolean value, otherwise C# compiler will give a compile-time error.

Example: Wrong if Statement
int i = 10, j = 20;

if (i + 1)
{
    Console.WriteLine("i is less than j");
}        

if (i + j)
{
    Console.WriteLine("i is greater than j");
}

You can call a function in the if statement that returns a boolean value.

Example: Calling Function as Condition
static void Main(string[] args)
{
    int i = 10, j = 20;

    if (isGreater(i, j))
    {
        Console.WriteLine("i is less than j");
    }        

    if (isGreater(j, i))
    {
        Console.WriteLine("j is greater than i");
    }
}

static bool isGreater(int i, int j)
{
    return i > j;                    
}

else if Statement

Multiple else if statements can be used after an if statement. It will only be executed when the if condition evaluates to false. So, either if or one of the else if statements can be executed, but not both.

Syntax:
if(condition1)
{
    // code block to be executed when if condition1 evaluates to true
}
else if(condition2)
{
    // code block to be executed when 
    //      condition1 evaluates to flase
    //      condition2 evaluates to true
}
else if(condition3)
{
    // code block to be executed when 
    //      condition1 evaluates to flase
    //      condition2 evaluates to false
    //      condition3 evaluates to true
}

The following example demonstrates else if statements.

Example: else if Statements
int i = 10, j = 20;

if (i == j)
{
    Console.WriteLine("i is equal to j");
}
else if (i > j)
{
    Console.WriteLine("i is greater than j");
}
else if (i < j)
{
    Console.WriteLine("i is less than j");
}
Output:
i is less than j

else Statement

The else statement can come only after if or else if statement and can be used only once in the if-else statements. The else statement cannot contain any condition and will be executed when all the previous if and else if conditions evaluate to false.

Example: else Statement
int i = 20, j = 20;

if (i > j)
{
    Console.WriteLine("i is greater than j");
}
else if (i < j)
{
    Console.WriteLine("i is less than j");
}
else
{
    Console.WriteLine("i is equal to j");
}
Output:
i is equal to j

Nested if Statements

C# supports if else statements inside another if else statements. This are called nested if else statements. The nested if statements make the code more readable.

Syntax:
if(condition1)
{
   if(condition2)
    {
        // code block to be executed when 
        //      condition1 and condition2 evaluates to true
    }
    else if(condition3)
    {
        if(condition4)
        {
            // code block to be executed when 
            //      only condition1, condition3, and condition4 evaluates to true
        }
        else if(condition5)
        {
            // code block to be executed when 
            //      only condition1, condition3, and condition5 evaluates to true
        }
        else
        {
            // code block to be executed when 
            //      condition1, and condition3 evaluates to true 
            //      condition4 and condition5 evaluates to false
        }
    }
}

The following example demonstrates the nested if else statements.

Example: Nested if else statements
int i = 10, j = 20;

if (i != j)
{
    if (i < j)
    {
        Console.WriteLine("i is less than j");
    }
    else if (i > j)
    {
       Console.WriteLine("i is greater than j");
    }
}
else
    Console.WriteLine("i is equal to j");
   
Output:
i is less than j

Use Ternary operator ?: instead of a simple if else statement.