C# if .. else and else if statement explained with 3 demos

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 on certain criteria, you can use the if, if … else and else…if statements to achieve that.

In the next section, you can see a few examples of using if statement but first, have a look at the syntax of using it:

General structure of C# if statement

You can use C sharp’s if statement as follows:

 

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

A simple example of using the if statement

Following is a simple example of using the if statement in C#. Two variables are declared and assigned the int values which is followed by the If statement. If the condition is true the statement inside the if statement should execute.

C# if statement

See online demo and code

The C# if else statement

In the above example, the code inside the if statement will execute 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 achieve that, the C# comes up with the else statement.

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

The if-else structure

The structure of the if else statement is:

 

In the above syntax, the code inside the else will execute if the given condition is false in the if section.

See the following example of using if else in C#.

An example of using the if else statement

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 should execute.

If the number is 50 or greater then the else block will execute. See the code and output of the above example by clicking the link below:

See online demo and code

The else if statement

Until now, we learned how to evaluate a single condition by using the if and else C# 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 and if orange then another block of code.

In that case, where we want to check various conditions, we can use if with else if statement.

Structure of the else if statement

Following is the general structure of the else if statement:

 

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

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 the selected color.

If you enter some other color, the else block will be executed with the message “Some other color!”. See the example by clicking the link below:

C sharp else-if

See online demo and code

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