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 types of statements used in decision making.

If Statement

If Else Statement

If, Else If Statement

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.

public class MyClass  
{  
    static void Main()  
    {  
        int a = 15;  
        int b = 12;  
        int result = a + b;  
  
        if (result == 20)  
        {  
            Console.WriteLine("Result value is 20");  
        }  
        if (result == 30)  
        {  
            Console.WriteLine("Result value is 30");  
        }  
    }      
}  

 

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.

public class MyClass  
{  
    static void Main()  
    {  
        int a = 20;  
        int b = 12;  
        int result = a - b;  
        if (result > 0)  
        {  
            Console.WriteLine("A is greater than B");  
        }  
        else  
        {  
            Console.WriteLine("A is less than B");  
        }  
    }  
} 

 

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.

public class MyClass  
{  
    static void Main()  
    {  
        int a = 20;  
        int b = 12;  
        int result = a - b;  
        if (result > 0)  
        {  
            Console.WriteLine("A is greater than B");  
        }  
        else if (result < 0)  
        {  
            Console.WriteLine("A is less than B");  
        }  
        else  
        {  
            Console.WriteLine("A is equals to B");  
  
        }  
    }  
}  

 

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:

public class MyClass  
{  
    static void Main(string[] args)  
    {  
        char grade = 'B';  
  
        switch (grade)  
        {  
            case 'A':  
                Console.WriteLine("Excellent!");  
                break;  
            case 'B':  
            case 'C':  
                Console.WriteLine("Well done");  
                break;  
            case 'D':  
                Console.WriteLine("You passed");  
                break;  
            case 'F':  
                Console.WriteLine("Better try again");  
                break;  
            default:  
                Console.WriteLine("You Failed!");  
                break;  
        }  
    }  
}  

 

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.