C# If Else Statement

C# is a modern programming language and supports common selection statements including the if..else statement. The code example in this article shows how to use an if..else statement in C#.

Introduction

 
C# is a common selection statement. The if..else statement checks a Boolean expression and executes the code based on if the expression is true or false. The if part of the code executes when the value of the expression is true. The else part of the code is executed when the value of the expression is false. The else part of the if..else is optional.
 
Here is the syntax of the if..else statement. 
  1. if (Boolean condition)  
  2.    {  
  3.       Statement  
  4.    }  
  5.    else  
  6.    {  
  7.       Statement  
  8. }  

If statement

 
The if section of the statement or statement block is executed when the condition is true; if it's false, control executes the code in the else statement or statement block. The ‘else’ portion of the statement is optional.
 
The following code example uses an if statement to check if the value of variable a is less than 0, then display a message, ‘a is negative’. 
  1. int a = -1;  
  2. if (a < 0)  
  3. {  
  4.    Console.WriteLine("a is negative.");  
  5. }  

If..else statement

 
As mentioned previously, the if statement goes with one or more else statements. If the “if” condition is not true, the program control goes to the “else” condition. The following code snippet uses a if..else statement to check if the value of a is less than 0. If not, then it displays a message, ‘a is 0 or positive’. 
  1. int a = -1;  
  2. if (a < 0)  
  3. {  
  4.    Console.WriteLine("a is negative.");  
  5. }  
  6. else  
  7. {  
  8.    Console.WriteLine("a is 0 or positive.");  
  9. }  
The {} brackets are optional for a single line statement. We can replace the above code with the following code. 
  1. if (a < 0)  
  2.    Console.WriteLine("a is negative.");  
  3. else  
  4.    Console.WriteLine("a is 0 or positive.");  

If..else..if statement

 
An if..else statement can have more if and else. The following code snippet uses an if.. and else if statement to check another condition. In this case, if the first if condition is not true, the program control goes to the next else..if condition and if this condition is not true also, then the control statement goes the last else part of the code. 
  1. // Read a  
  2. int a = 8;  
  3. if (a < 10)  
  4. {  
  5.    Console.WriteLine("a is less than 10.");  
  6. }  
  7. else if (a > 10 && a < 100)  
  8. {  
  9.    Console.WriteLine("a is between 10 and 100.");  
  10. }  
  11. else  
  12. {  
  13.    Console.WriteLine("a is greather than 100.");  
  14. }  

Nested if else

 
You can have nested if . . .else statements with one or more else blocks. Nested if can be used in both the if and else parts of the statement. The following code sample uses a nested if inside the else block. You can use multiple nested if statements.
 
  1. int a = -1;  
  2. if (a < 0)  
  3. {  
  4.    Console.WriteLine("a is negative.");  
  5. }  
  6. else  
  7. {  
  8.    if (a == 0)  
  9.       Console.WriteLine("a is 0.");  
  10.    else  
  11.       Console.WriteLine("a us positive.");  
  12. }  

Conditional operators in if

 
You can also apply conditional or ( || ) and conditional and (&&) operators to combine more then one condition. Listing 56 shows you how to use the if. . .else statement. 
  1. int a = -1; int b = 10; int c;  
  2. // Check if both numbers are negative  
  3. if (a < 0 && b < 0)  
  4. {  
  5.    Console.WriteLine("Both a and b are negative.");  
  6. }  
  7. else if (a < 0 || b < 0)  
  8. {  
  9.    Console.WriteLine("One number is negative.");  
  10. }  
  11. else  
  12. {  
  13.    Console.WriteLine("Both numbers are positive.");  
  14. }  
The if..else statement can have other nested statements and also can have more than one if..else statement. Listing 1 is a complete example of various use cases of the if..else statement. 
  1. using System;  
  2. class Program  
  3.    {  
  4.    static void Main(string[] args)  
  5.    {  
  6.       Console.WriteLine("Statements Sample!");  
  7.       int a = -1; int b = 10; int c;  
  8.       // Check if both numbers are negative  
  9.       if (a < 0 && b < 0)  
  10.       {  
  11.          Console.WriteLine("Both a and b are negative.");  
  12.       }  
  13.       else if (a < 0 || b < 0)  
  14.       {  
  15.          if (b > 0 && b <= 10)  
  16.          {  
  17.             c = a + b;  
  18.             Console.WriteLine("Total: {0}", c);  
  19.          }  
  20.       Console.WriteLine("One number is negative.");  
  21.       }  
  22.    else  
  23.    {  
  24.       Console.WriteLine("Both numbers are positive.");  
  25.       }  
  26.    Console.ReadKey();  
  27.   }  
  28. }  
Listing 1.
 
The output of Listing 1 looks like the following:
 
C# If Else
Figure 1.
 

Summary

 
In this article and code example, we learned how the if..else statement works in C#. We also saw the use of conditional if..else statements.