C# If-Else-If Statement with Examples

In c#, if-else-if statement or condition is used to define multiple conditions and execute only one matched condition based on our requirements.

 

Generally, in c# if statement or if-else statement is useful when we have a one condition to validate and execute the required block of statements. In case, if we have a multiple conditions to validate and execute only one block of code, then if-else-if statement is useful in our application.

Syntax of C# if-else-if Statement

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

 

if (condition_1)

{

// Statements to Execute if condition_1 is True

}

else if (condition_2)

{

// Statements to Execute if condition_2 is True

}

else if (condition_3)

{

// Statements to Execute if condition_3 is True

}

....

....

else{

// Statements to Execute if all conditions are False

}

If you observe above c# if-else-if statement syntax, we defined a multiple conditions to execute required statements.

 

Here the execution of if-else-if statement will starts from top to bottom and as soon as the condition returns true, then the code inside of if or else if block will be executed and control will come out of the loop.

 

In case, if none of the conditions return true, then the code inside of else block will be executed.

 

Following is the simple example of using if-else-if statement in c# programming language.

 

int x = 5;

if (x == 10)

{

Console.WriteLine("x value equals to 10");

}

else if (x > 10)

{

Console.WriteLine("x value greater than 10");

}

else

{

Console.WriteLine("x value less than 10");

}

If you observe above example, if-else-if statement will start the execution from top to bottom and will check if any condition is matching or not to execute the respective code block. In case if no condition is matching, then the else block will be executed.

C# If-Else-If Statement Flow Chart Diagram

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

 

C# If-Else-If Statement Flow Chart Diagram

 

If you observe above c# if-else-if statement flow chart, if the defined condition is true, then the statements within if condition will be executed otherwise it will move to another condition (else-if) to check whether the condition is matching or not. In case, if no conditions are matching then the else block will be executed.

C# If-Else-If Statement Example

Following is the example of defining if-else-if statement in c# programming language to execute the block of code or statements based on Boolean expression.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            int x = 5;

            if (x == 10)

            {

                Console.WriteLine("x value equals to 10");

            }

            else if (x > 10)

            {

                Console.WriteLine("x value greater than 10");

            }

            else

            {

                Console.WriteLine("x value less than 10");

            }

            Console.WriteLine("Press Enter Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above code, we defined an if-else-if conditions to execute the statements based on defined condition status.

Output of C# If Else If Statement Example 

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

 

C# If Else If Statement Example Result

 

If you observe above result, all defined conditions are failing due to that it executed an else block of statements and printed the required statement in console window.

 

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