C# if Statement

C# If..else statement - logo + graphic illustration

What is if Statement in C#?

The if is the decision-making statement in C sharp that enables executing a block of code if a certain condition is true.

You may place one block of code and evaluate whether the condition is True or False.

If the condition is True, the block of code is executed otherwise execution moves out of the if statement.

For executing a statement or block of code if the condition is false, you may use the Else statement.

If you have more possibilities and for each possibility, a specific block of code is required to execute then you may use the C# else If statement.

A simple example of using C# If statement

Let us start with a simple scenario; executing a block of code if the given condition is true.

For that, we have a variable x and assign it the value = 10.In the if condition, we will check if the value of x=10 then execute a statement.

If it is not equal to 10, nothing displays:

using System;

namespace ifStatement
{
    class Program
    {
        static void Main(string[] args)

        {

            int x;
            Console.WriteLine("Enter a number: ");

            x = int.Parse(Console.ReadLine());

            if (x < 50)
            {
               /* if you enter less than 50 this will be exeucted */
                Console.WriteLine("The entered number is less than 50!");
            }
            else

            {
                /* if you enter more than or equal to 50 this will be exeucted */
                Console.WriteLine("The entered number is greater than or equal to 50!");
            }
            Console.ReadLine();
        }
    }
}

The output of the above code is:

The if condition is True

As the value of x=10 so condition is True and the message is displayed.

The example of using C# Else statement

In this example, a message is displayed if the condition is false in the if statement. For that, the else statement is used and Console.WriteLine method is placed inside the else block that will execute if the condition is false.

using System;
namespace ifStatement

{

    class Program

    {
        static void Main(string[] args)
        {

            int y;
            y = 20;
            //Using if else statement

            if (y == 10)
            {
                Console.WriteLine("The value is 20 and condiiton is True");
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("The value of y is not 20 so condition is false");
            }
            Console.ReadLine();
        }
    }
}

The output:

The value of y is not 20 so condition is false

As the value of the variable y is not 20, the condition is false, thus, the statement in the else block is executed.

The example of else if statement

As mentioned earlier, the else if statement is used when you have more than two blocks of code that you want to execute for different conditions.

For example, if the color is green then execute block A, if red then block B. Similarly, for black color, execute block C, and for any other color, execute block D.

The general structure of the else if statement is as follows:

if (condition-1)

{
                // Execute block A
}

else if (Condition-2)
{
                // Execute block B
}
else if (Condition-3)
{
                // Execute block C
}
else
{
                // Execute block D
}

An example of else if statement

In this example, a variable x is assigned a value. If the value is 10 then first if condition will be true and the message inside this will display.

If the value of the variable is 20 then the second condition in the else if will be true and its message will display. Similarly, the third condition is to check if the value is 30.

For any other value, the else statement will execute and the message inside the else block will execute.

using System;

namespace ifStatement
{
    class Program
    {
        static void Main(string[] args)
        {
            int y;
            y = 30;


            //Using if else statement
            if (y == 10)

            {
                Console.WriteLine("Value of y = 10; First condition is True");
                Console.ReadLine();
            }

            else if (y == 20)
            {
                Console.WriteLine("Value of y = 20; Second condition is True");
                Console.ReadLine();
            }
            else if (y == 30)
            {
                Console.WriteLine("Value of y = 30; Third condition is True");
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("None of the conditions True, so else block executed!");
            }

            Console.ReadLine();
        }
    }
}

The output of the above code:

Value of y = 30; Third condition is True

The example of using string in if statement

The following example shows you using the same color example with the C# if else statement as I mentioned in the above example.

If the value of the string variable is red then the first condition will be true and its message will display.Similarly, the message for green and blue values will be displayed.

In the end, the else statement will execute for any other value i.e. all the above conditions are false.

using System;
namespace ifStatement
{
    class Program
    {
        static void Main(string[] args)
        {
            string sel_color = "Green";

            //Using if else statement
            if (sel_color == "Red")

            {
                Console.WriteLine("The Color is Red!");
                Console.ReadLine();
            }

            else if (sel_color == "Green")
            {

                Console.WriteLine("The Color is Green!");
                Console.ReadLine();

            }

            else if (sel_color == "Blue")

            {

                Console.WriteLine("The Color is Blue!");
                Console.ReadLine();

            }

            else

            {
               Console.WriteLine("All conditions failed");
            }
            Console.ReadLine();
        }
    }
}

The output:

The Color is Green!

The example of using ‘AND’ operator in if / else and if..else statement

To understand the else if statement in C#, consider we have a variable x.

  • The value to the variable x is assigned by user input, which must be numeric.
  • If the value is less than or equal to 10 then first if condition will be true and the message inside that if block will execute.
  • If the value is greater than 10 or less than 20 then the second if statement (else if) will execute and display its statement.
  • The third condition is to check the value between 21 and 30.
  • Finally, we have an else statement that will execute if the value is greater than 30:
using System;
namespace ifStatement

{
    class Program
    {
        static void Main(string[] args)
        {
            int y;
            y = 18;

            //else if with range of values example
            if (y > 0 && y <= 10)

            {
                Console.WriteLine("The value of y is between 0 and 10");
                Console.ReadLine();
            }

            else if (y >= 11  && y <= 20)

            {

                Console.WriteLine("The value of y is between 11 and 20");
                Console.ReadLine();

            }

            else if (y >=21 && y <= 30)
            {
                Console.WriteLine("The value of y is between 21 and 30");
                Console.ReadLine();
            }

            else
            {
                Console.WriteLine("Above 30");
            }
            Console.ReadLine();
        }
    }
}

The output as the value of the variable set = 18:

The value of y is between 11 and 20
Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!