As we all make decisions in our real life, similarly in the logical world of programming, decisions are an essential part of executing a specific block of code based on the fulfillment of the condition. The control statements control the flow of statements and based on certain conditions; different logical blocks are executed. The programming language provides all these things as a form of conditional statements or decision-making statements, which will be covered in this chapter.



What Is Decision Making Statements in C#?

These types of statements are used by programmers to determine one or more conditions evaluated by the program at run-time. Specific blocks of code associated with these statements will be executed only when the condition is determined.

The flowchart of the Decision-making technique in C can be expressed as:

Decision Making

Decision Making Statements in C#:
  • if statement
  • if-else statement
  • if-else-if statement
  • Nested if statement
  • switch statement

if statement

The "if statement" will ensure the given condition, and if the condition becomes true, then the block of statements enclosed within curly braces will get executed; otherwise, not.

Syntax:

if(condition)
{  
        // code for execution  
}

if-else statement

The "if statement" will ensure the given condition and if the condition becomes true, then the block of statements enclosed within curly braces will get executed; otherwise, the block of code associated with the else will get executed.

Syntax:

if(condition)
{  
        // if condition is true 
}
else
{  
        // if the condition becomes false 
}

if-else-if statement

The "if-else-if" ladder will execute a single block based on one condition from multiple conditional statements. The conditions are checked one by one in the if statements from the top and will execute that block whose condition is evaluated to true.

Syntax:

if(first-condition) {  
        // when first condition becomes true 
}
else if(second-condition)
{  
        // when second condition becomes true 
}  
else if(third-condition)
{  
        // when third condition becomes true  
}  
else
{
        // when all the conditions are false  
}

Nested if statement

In this type of conditional statement, one if statement will be nested or inside another if or else statement. Here multiple conditions have to be evaluated as true only then the nested block associated with multiple "if conditions" will be executed.

Syntax:

if (first-condition) 
{
        // when first condition becomes true 
        if (second-condition) 
        {
                // code to be executed 
                // if second condition becomes true 
        }
}

Switch statement

Switch statement acts as an alternative to the "if-else-if" statement. Here, an expression is checked based on different cases and executed that particular block that matches the case. Each case is exited by a break statement that helps the program flow move out of the switch case blocks.

Syntax:

switch (expression)
{
        case value1:    // statements
        break;

        case value2:    // statements
        break;

        case valueN:    // statements
        break;
        default:                // default statements
}

Nested switch statement

Like nested "if-else", it is also possible to write a switch statement within another switch statement.


Program of if-else-if With Nested if

Example:

using System;
namespace Dec_making
{

class IfConditions
{
        public static void Main(string[] args)
        {
                int number = 20;
                if (number < 5)
                {
                        Console.WriteLine("{0} is less than 5", number);
                }
                else
                {
                    if(number < 10)
                        {
                                Console.WriteLine("{0} is less than 10", number);
                        }
                        else
                            Console.WriteLine("{0} is greater than 10", number);
                }

                Console.WriteLine(" Lastly, this statement will be executed.");
        }
}

}

Output:

20 is greater than 10
 Lastly, this statement will be executed.

Program of the Switch Case

Example:

using System;
namespace NestedSwitch
{

class Program
{
    static void Main(string[] args)
    {
        string title = "student";
        int grade = 1;
        switch( title ) {
            case "teacher":
                Console.WriteLine("You are a teacher.");
                break;
            case "student":
                Console.WriteLine("You are a student.");
                switch( grade ){
                    case 1:
                        Console.WriteLine(" You are in pre-school ");
                        break;
                    case 2:
                        Console.WriteLine(" You are in high-school ");
                        break;
                    case 3:
                        Console.WriteLine(" You are in college ");
                        break;
                    case 4:
                        Console.WriteLine(" You are in university ");
                        break;
                    default:
                        Console.WriteLine("Result Unknown");
                        break;
                }
                break;
            default:
                Console.WriteLine(" Office Staff ");
                break;
        }
        Console.Read();
    }
}

}

Output:

You are a student.
 You are in pre-school


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram