Hit enter after type your search item

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 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 assigned 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:


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.


The output:

The value of y is not 20 so condition is false

As the value of the variable y is not 20 so 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 the block C and for any other color, execute the block D.

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

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.


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 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 display.

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


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, that 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 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:


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

The value of y is between 11 and 20

This div height required for enabling the sticky sidebar