If-else-switch statements in C# - ProgramIdea

Selection Statements


Selection statements used for making a decision based on a condition. If, else and switch keywords are used for making a decision based control flow. We can run the specific block of codes according to the condition satisfication.

If statement

If statement executes the block of statements only when condition is true. If boolean condition is true then, it executes block of statements inside if block, and if boolean condition false then, it skip the if-block statement.

// Syntax of if-block

 

// if(condition)

//{

//    statemments

//}       

 

// if statement

int count = 5; 

if (count == 5)

{

    Console.Write("Condition is true");

}

 

// Output

// Condition is true

If-else statement

If boolean condition is true then, it executes if-block of statements and if boolean condition is false then if-block is skip and else-block is executed.

int count = 5;

 

// if-else statement

if (count == 6)

{

    Console.WriteLine("Condition is true");

}

else

{

    Console.WriteLine("Condition is false");

}

 

// Output

// Condition is false

In if-statement contains only one statement then opening and closing brackets are optional.

int count = 5;

// If- statement without { } bracket

if (count == 5)

    Console.Write("Condition is true");

           

 

// Output

// Condition is true

Nested if-else statement

If-else block can be defined inside the if-else block that is known as nested if-else block.

    // Nested if-else statenment

    int count = 5;

    if (count >= 5)

    {

         if (count == 5)

         {

             Console.WriteLine("Count is 5");

         }

         else

         {

             Console.WriteLine("Count is greater than 5");

         }

    }

    else

    {

        Console.WriteLine("Count is less than 5");

    }

 

     // Output

     // Count is 5

If-else-if statement

Multiple if-else blocks are replaced by if-else-if block. Multiple conditions can be checked by if-else-if blocks. In if-else-if block can contains only one if-block, one or more else-if block and only one else-block that is optional.

      // if else if

      int count = 10;

        if (count == 5)

        {

            Console.WriteLine("Count is 5");

        }

        else if (count > 5)

        {

            Console.WriteLine("Count is greater than 5");

        }

        else

        {

            Console.WriteLine("Count is less than 5");

        }

 

        // Output

        // Count is greater than 5

Switch statement

Switch statement is an alternate option of if-else-if statement. In switch statement, you have to pass the variable in switch clause. After that checking the variable value with each case label one by one until that matches with case label. Once matches then execute the case statement codes and break statement transfer control to the outside of switch statement. If no case label matches with variable then control transfer to default-section, if default-section is there otherwise control transfer to outside the switch-statement.

        // Switch statement

        int count = 5;

        switch (count)

        {

            case 1: Console.WriteLine("One");

                break;

            case 2: Console.WriteLine("Two");

                break;

            case 3: Console.WriteLine("Three");

                break;

            case 4: Console.WriteLine("Four");

                break;

            case 5: Console.WriteLine("Five");

                break;

            default: Console.WriteLine( "Count is greater than 5" );

                break;

        }

 

        // Output

        // Five