Conditional Statements In C#

Today, I am going to show you how we can create logical decision making statements in the code which will help you to make decision based on certain conditions. On the basis of condition you can go to next steps. There are different type of statements used in decision making.

  1. If Statement
  2. If Else Statement
  3. If, Else If Statement
  4. Switch Case Statement

If statement

“If” statement always used to check the value is true or not. If the value is true, then it will execute, otherwise it will not. It works on the basis of Boolean expression. If the value is true, then if block is executed otherwise next statement(s) would be executed.

We can create multiple if statements in a program as well as also create nested “IF”.

Let's take an example to understand the “IF” statement.

  1. public class MyClass  
  2. {  
  3.     static void Main()  
  4.     {  
  5.         int a = 15;  
  6.         int b = 12;  
  7.         int result = a + b;  
  8.   
  9.         if (result == 20)  
  10.         {  
  11.             Console.WriteLine("Result value is 20");  
  12.         }  
  13.         if (result == 30)  
  14.         {  
  15.             Console.WriteLine("Result value is 30");  
  16.         }  
  17.     }      
  18. }  
If Else statement

Sometime it is required to show different value if the value is not true. We need to write some different logic for false statement. In that scenario, we can use If-Else statement. The main purpose is handling to false statement. “If” is executed when value is true and if false then “else” statement will be executed.

Let's take one more example to understand the If-Else statement.
  1. public class MyClass  
  2. {  
  3.     static void Main()  
  4.     {  
  5.         int a = 20;  
  6.         int b = 12;  
  7.         int result = a - b;  
  8.         if (result > 0)  
  9.         {  
  10.             Console.WriteLine("A is greater than B");  
  11.         }  
  12.         else  
  13.         {  
  14.             Console.WriteLine("A is less than B");  
  15.         }  
  16.     }  
  17. }  
If, Else-If statement

There are some scenarios where we need to check the value multiple times, so in that case you can use if, else if, else. It is a way to execute the login through different module and check that value is true or not. On the basis of value we perform some action.

If the first if statement meet the result then code inside the “if” block executes otherwise it will check next inside the “else-if”. This continues as a series of else if statements. A default else code block may execute when no condition has been evaluated to true. Let's take one more example to understand the If-Else-If statement.
  1. public class MyClass  
  2. {  
  3.     static void Main()  
  4.     {  
  5.         int a = 20;  
  6.         int b = 12;  
  7.         int result = a - b;  
  8.         if (result > 0)  
  9.         {  
  10.             Console.WriteLine("A is greater than B");  
  11.         }  
  12.         else if (result < 0)  
  13.         {  
  14.             Console.WriteLine("A is less than B");  
  15.         }  
  16.         else  
  17.         {  
  18.             Console.WriteLine("A is equals to B");  
  19.   
  20.         }  
  21.     }  
  22. }  
Switch Case statement

Switch statement is the short and better way to execute the If-Else-If statement. If we check the value multiple times, then the process goes to complex and performance would be slow. In that case we use switch case statement which acts as a substitute for long If-Else-If ladder that is used to test a series of conditions. A switch statement contains one or more case labels which are tested against the switch expression. If value is matched with particular case then only that case will be executed and rest of all will be untouched.

Let's take an example to understand:
  1. public class MyClass  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         char grade = 'B';  
  6.   
  7.         switch (grade)  
  8.         {  
  9.             case 'A':  
  10.                 Console.WriteLine("Excellent!");  
  11.                 break;  
  12.             case 'B':  
  13.             case 'C':  
  14.                 Console.WriteLine("Well done");  
  15.                 break;  
  16.             case 'D':  
  17.                 Console.WriteLine("You passed");  
  18.                 break;  
  19.             case 'F':  
  20.                 Console.WriteLine("Better try again");  
  21.                 break;  
  22.             default:  
  23.                 Console.WriteLine("You Failed!");  
  24.                 break;  
  25.         }  
  26.     }  
  27. }  
In this article, you learned how to handle conditional statement and which statement will be useful in which scenario.

Hope you all enjoyed reading the article.

 

Up Next
    Ebook Download
    View all
    FileInfo in C#
    Read by 9.7k people
    Download Now!
    Learn
    View all