If Else in C# – Includes If-Else Ladder

(Last Updated On: February 5, 2019)

Learn if else in C# including if else ladder and nested if else conditional statements with simple explanation and program example.

If else in C# programming is a decision-making statement / condition followed by a code block that is used in the program. Depending on evaluation of Boolean expression or conditions to true or false, respective code blocks executed.

In real life we make some decision based on condition. Don’t we? For example,

Similarly, we use conditional statements if or if-else in C# programs.

 

If Statement in C#

Syntax of if statement

C# code example

In below program, since expression (a < 20) inside it is true as value of variable a is 10 that is less than 20, the control will go inside the if block and execute its all statements.

Output:
Inside if block
Done

If the expression (a < 20) is false the code will skip the if block and output of above program will be as below

Output:
Done

 

If Else in C#

In if else block, expression in if condition is evaluated to true or false. If expression is true, then control will execute the if block or control will jump to else block and execute it.

Syntax of if else in C#:

Code Example:

In this program, value of a is 10. So, expression in if (a ==10), becomes true. Hence, the control will go inside if block resulting output to Even.

Output:
Even

In above program, change value of variable e.g. int a =11, then control will come in else block as in if condition, a ==10 will be false. As an output the program will print Odd.

Output
Odd

 

If Else Ladder in C#

If else ladder has “if, multiple else if and else blocks”. You can see the syntax below.

Syntax:

The control start checking every if conditions till it find true. once it finds true, it run that if block otherwise it will run else block. It run the else block written in last if no conditions are true.

For example,

first control check if(Expression 1). If true, execute the block and exit the entire if-else ladder.

If the statement if(Expression 1) false, then control check if(Expression 2). If true run the block and exit if else ladder. If false, it check the next.

And so on…..

If none of the conditions / expressions are true, then control goes to last else block.

 

If Else Ladder Code Example in C#

For better understanding, let’s consider below program, where depending upon the value of day number, it will print the correct day. Day number starts from 1 that

represent Monday, 2 represents Tuesday and so on.

First, get overview of the if else ladder code, then let’s test it.

Test-1

modify the value of day = 1.

When you run , control will check if (day == 1). It found it true, so, the control execute the code block, print the output as Monday. Exit the entire if else ladder and print “End”

So, Output of the program will be
Mondyay
End

TEST -2

modify the value of day = 3.

control will check if (day == 1). its false, so, control checks if (day == 2). Again false. it checks next if (day == 3). it found true. so, it will print Wednesday and exit the ladder printing “End”

So, Output of the program will be
Wednesday
End

TEST -3

modify the value of day = 9.

Control will check all conditions one by one i.e. if (day == 1)., if (day == 2)., if (day == 3)., if (day == 5).

Since, it found none of the conditions are true, hence it will go to else block, and print Day not found!!!

Output:
Day not found!!!
End

Nested If and Nested If Else in C#

Nested if or nested if else in C# programming is simply writing an if or if else conditions inside another if or else block.

We can also say that, writing on if else block inside another if else block. Some time it is referred as outer and inner if else block.

Code example:

In above program, you can also write inner if else inside an outer if else.

Code example: If else block inside a if block.

NOTE: Note that if outer if condition is false, then compiler will not go inside its block. hence, it can also not go inside the inner if else block.

Recommendation: Copy the above code in Visual Studio editor. Run and see the outputs. Also, try modifying value of variable a and see the result.