C # Case Control Structure

While writing code for software it the common task to check some conditions and take actions according to that conditions. In C# we can achieve this with the help of following decision-making statements.

  • If  statement:
  • If-else statement
  • If-else-if ladder  statement
  • Switch case
  1. If statement:

An ‘if’ statement is the most common form of decision-making statements.

The general syntax is as below:


if (Boolean expression)
{
  //Statements to be executed if condition is true
}

 

The ‘if’ statement takes expression which is evaluated to a boolean value (true/ false). If the expression evaluates to true then the statements inside the curly braces are executed otherwise it is skipped. The expression must produce the result of bool type. If there is only one statement to be executed then one can omit the curly braces and write the condition as below:


if(Boolean expression) 
//Statement to be executed if condition is true

 But it is good to practice to use curly braces. Most of the time logical and relational operators are used in an expression to check conditions.

 

Let’s see following example:

 

using System;
namespace IfStatement
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 10;
            int b = 20;
            int c = 10;
            if (a == b)
           {
              Console.WriteLine("The value a is equal to the value of b.");
           }
           if (a == c)
           {
              Console.WriteLine("The value a is equal to the value of c.");
           }
           Console.ReadLine();
         }
    }
}

 

Once the above code is compiled and executed, it will produce the following result –

The output of above program is = The value a is equal to the value of c.
  1. If-else statement:

In ‘if-else’ statement in if block the statements are executed when the expression evaluates to true but ‘if-else’ structure provides the way so that we can execute statements when the expression evaluates to false also. The basic syntax is as below:

     if (Boolean expression)
     { 
        //Statements to be executed if condition is true
     }
     else
     {
        //Statements to be executed if condition is false
     }

 

Recommended Post   Anonymous Classes and Methods in C#

The first block of statement is executed when if the expression is true otherwise the else part is executed.

 

Let’s see following example

 

using System;
namespace IfElseStatement
{
   class Program
   {
      static void Main(string[] args)
      {
         int a = 10;
         int b = 20;
         if (a == b)
         {
            Console.WriteLine("The value of a is equal to the value b.");
         }
         else
         {
            Console.WriteLine("The value of a is not equal to the value b.");
         }
         Console.ReadLine();
      }
   }
}

Once the above code is compiled and executed, it will produce the following result –

The output of above program is value
The Value of a is not equal to the value b.
  1.  If-else-if ladder statement: 

In above ‘if’ and ‘if-else’ statements we can check only one condition but in real there is the situation where we want to check multiple conditions like if the first condition is false then check second and so on. In this type of situation, ‘if-else-if’ statement is useful.

The syntax is as below:

if (Boolean expression1)
{
   //Statements to be executed if condition1 is true
}
else if (Boolean expression2)
{
    //Statements to be executed if condition2 is true
}
else if (Boolean expression3)
{
    //Statements to be executed if condition3 is true
}
.
.
.
else
{
   //Statements to be executed if all conditions false
}

Once any boolean expression evaluates to true then that statement block is executed and all other if block and else part are skipped. If all the boolean expression evaluates to false then else part is executed.

Let’s see following example-

using System;
namespace LadderIfElse
{
   class Program
   {
     static void Main(string[] args)
     {
        int a = 4;
        if (a == 1)
       {
         Console.WriteLine("The value of variable a is one.");
       }
       else if (a == 2)
       {
         Console.WriteLine("The value of variable a is two.");
       }
       else if (a == 3)
      {
         Console.WriteLine("The value of variable a is three.");
      }
      else if (a == 4)
      {
         Console.WriteLine("The value of variable a is four.");
      }
      else if (a == 5)
      {
         Console.WriteLine("The value of variable a is five.");
      }
      else
     {
        Console.WriteLine("The value of variable a is not between 1 to 5.");
     }
     Console.ReadLine();
   }
 }
}

Once the above code is compiled and executed, it will produce the following result –

The output of above program is
The value of variable a is four.
  1. Switch statement:

    The switch statement provides the way to select one option from available multiple options depending upon condition. It is like ‘if-else if- else’ statement but in some cases using the switch is more efficient.

    In switch statement, the value of an expression is checked with multiple case constants if match found then that statement block is executed. The general syntax of switch statement is as below:

switch(expression)
{
   case constant1:
   //Statements to be executed
   break;
   case constant2:
   //Statements to be executed
   break;
   case constant3:
   //Statements to be executed
   break;
   .
   .
   .
  default:
  //Statements to be executed
  break;
}


Some points to keep in mind

  • The expression in a switch is must be of an integer type like int, short, byte, and char or of string type.
  • The value of case constants must be constant or literal. The type of constant must be the same as that of expression.
  •  The values of constants must be unique.
  • In C# it is compulsory to end each case with ‘break’ statement. If you don’t write ‘break’ then the compiler will generate an error. The ‘break’ statement causes the termination of switch.
  • The default case is optional. The default part is executed if all cases fail.
  • It is not necessary to write default at the end of switch block. One can write default at the top of all cases, it does not change the behavior of default block. But is good practice to write default at the end.

Let’s see following example:

using System;
namespace SwitchCase
{
   class Program
  {
    static void Main(string[] args)
    {
       int a = 6;
       switch (a)
       {
          case 1:
          Console.WriteLine("The value of variable a is one.");
          break;
          case 2:
          Console.WriteLine("The value of variable a is two.");
          break;
          case 3:
          Console.WriteLine("The value of variable a is three.");
          break;
          case 4:
          Console.WriteLine("The value of variable a is four.");
          break;
          case 5:
          Console.WriteLine("The value of variable a is five.");
          break;
          default:
         Console.WriteLine("The value of variable a is not between 1 to 5.");
         break;
      }
      Console.ReadLine();
    }
  }
}

Once the above code is compiled and executed, it will produce the following result –

The output of above program is 
The value of variable a is not between 1 to 5.