3 demos to learn C# if .. else and else if

The if statement of C#

The if is a decision-making statement in C# programming which is used to execute a block of code if a given condition evaluates as true. In other words, if you have more than one options and you have to choose one based at certain criteria you can use if, if … else and else…if statements to achieve that.

Following are a few examples of using C# if statement, lets us first look at the general structure to use it.

General structure of C# if

You can use C sharp if statement as follows:

If (condition/expression)

{

// Code to be executed if condition is true

}

The above if statement structure is to evaluate only one option. If this turns out to be true the code will be executed inside curly braces. Otherwise, execution will move to next statements after if statement.

An if statement example

Following is a simple example of using the if statement. Two variables are declared and assigned int values followed by the If statement. If the condition is true the statement inside the if statement will be executed.

C# if else statement

In the above example, the code will be executed only if the condition is true. If the condition is false the execution will be moved to next line of code, outside of the if block. Generally you want to show some message or perform some action if a condition is false in the if statement. In order to do that C# comes up with the else statement.

Following is the general structure of the if..else:

The if else structure

The structure of if else, C sharp is:

If (condition/expression)

{

// Code to be executed if condition is true

}

else

{

// Code to be executed if condition is false

}

In the above syntax, the code inside else will be executed if the given condition is false in the if section. See the following example of using if else in C#.

if else C# example

Following is a simple if else example. As you run the code it will ask you to enter a number. The code will check the entered number in the if else statement. If the number is less than 50, the statement inside the if statement will be executed. If the number is 50 or more then the else block will be executed. See code and output of above example by clicking the link below:

The else if statement

Until now, we learned how to evaluate a single condition by using if and else statements. In many situations, you have multiple options or conditions to check and only executing that turns out as true. For example if the color is green then execute one block of code, if red then another block if orange then another block of code and so on.  In that case where we want to check various conditions, we can use C# else if statement.

Structure of else if C#

Following is the general structure of else if statement:

If (condition/expression)

{

// Code to be executed if the condition is true

}

else If (condition/expression)

{

// Code to be executed if the second condition is true

}

else If (condition/expression)

{

// Code to be executed if third condition is true

}

else

{

// Code to be executed if condition is false

}

You can see, we used three conditions in single if statement by using else if. Note that:

As a condition is met the execution will be moved out of if statement after executing any given statements inside that else if block.

If none of the conditions is true the else block will be executed.

Example of using else if

Following is an example of using else if (for various conditions in single if statement). As you run the code it will ask you to enter a color name. If you enter black, white or orange and press the enter key, the message will be shown for selected color. If you enter some other color the else block will be executed with the message “Some other color!”. See example by clicking the link below:

Only enter colors (black, white and orange) in small letters in order to check C sharp else if block as true.

Also see: C# Switch

Was this article helpful?

Related Articles

Leave A Comment?