Condition Statements in C#

Control statements are the statements which are used to control the flow of the program. Many times we need to make some decision, skip some code, repeat the  same code, or switch from one block to other block of code. Control statements are of two types:

Selection Statements

The selection statements are blocks of code which execute after a condition is satisfied. It consists of:

if statement:

If statements hold the condition to be checked for a piece of code to execute. If the condition is true then it will execute the if part otherwise it skips the bock of code inside if and jumps to the else statement (if else block is available). The syntax of the if statement is:

If(Condition)
{
   //Do something;
}
Else
{
   //Else work;
}


Example:

  1. If(answer == ”yes”)  
  2. {  
  3.     Console.WriteLine(“YES”);  
  4. }  
  5. Else  
  6. {  
  7.     Console.WriteLine(“No”);  
  8. }  
Here, if the resultant answer will be yes then YES will be printed otherwise NO will get printed.

Switch statement:

When a block of code is needed to be executed based on some matching context , then those statements are called switch statements. When multiple statements are there depending on a variable, or there are many results on different values of a variable then a switch case statement is used. Its syntax is:

 

Switch(value)
{
   Case1:
   {
      //Do something;
      break;
   }
   Case2:
   {
      //Do work;
      break;
   }……
   Casen:
   {
      //Do work;
      break;
   }
   default:
   {
      //work;
      break;
   }
}

Example:

  1. Switch(roll)  
  2. {  
  3.     Case1:  
  4.     {  
  5.         Console.WriteLine(“student 1”);  
  6.         break;  
  7.     }  
  8.     Case2:  
  9.     {  
  10.         Console.WriteLine(“student 2”);  
  11.         break;  
  12.     }  
  13.     default:  
  14.     {  
  15.         Console.WriteLine(“student”);  
  16.         break;  
  17.     }  
  18. }  
Here, the print value will be according to the input roll no. and if the input roll number does not match any of the case, the default value will be printed.

Iterative Statements:

When we want to repeat a block of code more than once, the statements which iterate these code are called iterative statements. We can repeat a block of code zero or more times & each time it executes it is counted as one iteration. They are of four types:
  1. While Statement: If a block of code is to be executed again & again till the condition is true , we use while statement. The syntax is:

    while(condition)
    {
       //repeat the code until the condition is false.
    }


    Example:
    1. while(counter>10)  
    2. {  
    3.    counter++;  
    4. }  
    Here, the counter will gert increased until it is less than 10.

  2. Do While Statement: If a block of code is to be executed again & again till the condition is true , but it should execute at least once, we use do while statement.

    do
    {
       //repeat the code until the condition is false.
    }
    while(condition)

    Example:
    1. do {  
    2.     counter++;  
    3. }  
    4. while (counter > 10)  
    Here, the counter will increase at least once & then if condition is satisfied then it will increase till it reaches 10.

  3. For Statement: Instead of while block we can also use for statement where we repeat the code by initializing a variable, putting condition for the iteration & incrementing it. The syntax is:

    for (initialization; condition; incrementation)
    {
       // do work;
    }

    Example:
    1. for (i=0; i<10; i++)  
    2. {  
    3.    Console.WriteLine(“repeat”);  
    4. }  
    Here, i is initialized to 0 then it prints repeat and then I is incremented by 1 and again the loop continues until it reaches 10. It will print repeat till i is 9 , when it reaches 10 it breaks and come out of the loop.

  4. foreach Statement: It is similar to for statement but it can loop through collections such as arrays. Its syntax is:

    foreach(var data in collection)
    {
       //do work
    }

    Example:
    1. foreach (var i in arr)  
    2. {  
    3.    Console.WriteLine(“arr[i]”);  
    4. }  
    Here, i is the count of the total values in array arr and  it will print each array value one by one.

Hope this blog has helped you in understanding conditional statements.