Conditional Statements in C#

A statement that can be executed based on a condition is known as a “Conditional Statement”. The statement is often a block of code.

The following are the 2 types:

  1. Conditional Branching
  2. Conditional Looping

Conditional Branching

This statement allows you to branch your code depending on whether or not a certain condition is met.

In C# are the following 2 conditional branching statements:

  1. IF statement
  2. Switch statement

IF Statement

The if statement allows you to test whether or not a specific condition is met.

Syntax

  1. If(<Condition>)  
  2. <statements>;  
  3. Else if(<Condition>)  
  4. <statements>;  
  5. ---------------------  
  6. -----------------------  
  7. Else  
  8. <statements>;  
Example

W.A.P to find the greatest number using an if statement:
  1. using System;  
  2. class ifdemo  
  3. {  
  4.   public static void Main()  
  5.   {  
  6.   int a,b;  
  7.   Console.WriteLine("enter 2 no ");  
  8.    a=Int.Parse (Console.ReadLine());  
  9.   b=Int.Parse(Console.ReadLine());  
  10.   if(a>b)  
  11.      {  
  12.        Console.WriteLine("a is greather");  
  13.       }  
  14.   else If(a< b)  
  15.       {  
  16.       Console.WriteLine("b is greather");  
  17.       }  
  18.   else   
  19.      {  
  20.     Console.WriteLine("both are Equals");  
  21.      }  
  22. Console.ReadLine();  
  23. }  
Switch Statement

The switch statement compares two logical expressions.

Syntax
  1. Switch(<Expression>)  
  2. {  
  3. Case <Value> :  
  4. <stmts>  
  5. Break;  
  6. -----------------------  
  7. -------------------------  
  8. ------------------------  
  9. Default :  
  10. <stmts>  
  11. Break;  
  12. }     

 

Note : In the case of the C# Language, using a break after every case block is mandatory, even for the default.

Example

W.A.P to choose a color using a switch case.

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace ConditionalStatementDemo  
  8. {  
  9.     class Switchdemo  
  10.     {  
  11.         int ch;  
  12.         public void getdata()  
  13.         {  
  14.             Console.WriteLine("choose the following color");  
  15.             ch = int.Parse(Console.ReadLine());  
  16.             switch (ch)  
  17.             {  
  18.                 case 1:  
  19.                     Console.WriteLine("you choose Red");  
  20.                     break;  
  21.                 case 2 :  
  22.                      Console.WriteLine("you choose Green");  
  23.                     break;  
  24.                 case 3:  
  25.                     Console.WriteLine("you choose Pink");  
  26.                     break;  
  27.                 default:  
  28.                     Console.WriteLine("you cant choose correct color");  
  29.                     break;  
  30.   
  31.   
  32.             }  
  33.         }  
  34.         public static void Main()  
  35.         {  
  36.             Switchdemo obj = new Switchdemo();  
  37.             obj.getdata();  
  38.             Console.ReadLine();  
  39.   
  40.         }  
  41.     }  
  42. }  
Conditional Loops

 C# provides 4 loops that allow you to execute a block of code repeatedly until a certain condition is met; they are:

  • For Loop
  • While loop
  • Do ... While Loop
  • Foreach Loop

Each and every loop requires the following 3 things in common.

  1. Initialization: that sets a starting point of the loop
  2. Condition: that sets an ending point of the loop
  3. Iteration: that provides each level, either in the forward or backward direction

Direction

Syntax

  1. For(initializar;Conition;iterator)  
  2. {  
  3. < statement >  
  4. }  
Example
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace ConditionalStatementDemo  
  8. {  
  9.     class ForLoop  
  10.     {  
  11.          
  12.         public void getdata()  
  13.         {  
  14.               
  15.             for (int i = 0; i <= 50; i++)  
  16.             {  
  17.                 Console.WriteLine(i);  
  18.             }  
  19.         }  
  20.         public static void Main()  
  21.         {  
  22.             ForLoop f = new ForLoop();  
  23.             f.getdata();  
  24.             Console.ReadLine();  
  25.         }  
  26.     }  
  27. }  
While Loop

Syntax
  1. While(Condition)  
  2. {  
  3. < statement >  
  4. }  
Example
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace ConditionalStatementDemo  
  8. {  
  9.     class WhileDemo  
  10.     {  
  11.         int x;  
  12.         public void whiledemo()  
  13.         {  
  14.             while (x <= 50)  
  15.             {  
  16.                 Console.WriteLine(x);  
  17.                 x++;  
  18.             }  
  19.         }  
  20.         public static void Main()  
  21.         {  
  22.             WhileDemo obj = new WhileDemo();  
  23.             obj.whiledemo();  
  24.             Console.ReadLine();  
  25.         }  
  26.     }  
  27. }  
Do ... While Loop

Syntax

  1. Do  
  2. {  
  3. < statement >  
  4. }  
  5. While(Condition)  
Example 

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace ConditionalStatementDemo  
  8. {  
  9.     class DoWhileDemo  
  10.     {  
  11.         int x;  
  12.         public void does()  
  13.     {  
  14.         do  
  15.         {  
  16.             Console.WriteLine(x);  
  17.             x++;  
  18.         }  
  19.         while (x <= 50);  
  20.             
  21.     }  
  22.             
  23.   
  24.         public static void Main()  
  25.         {  
  26.             DoWhileDemo obj = new DoWhileDemo();  
  27.             obj.does();  
  28.             Console.ReadLine();  
  29.         }  
  30.     }  
  31. }  
In the case of a for and a while loop from the first execution there will be condition verification.

But in the case of a do .. while, the condition is verified only after the first execution, so a minimum numbrr of executions occur. In the case of for and while the minimum numbrr of executions will be zero whereas it is 1 in the case of a do-while loop.

Foreach Loop

It is specially designed for accessing the values of an array and collection.

Syntax
  1. Foreach(type var in coll/Arr)  
  2. {  
  3. < statement >;  
  4. }  
Your feedback and suggestion is always welcome by me.