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

If(<Condition>)  
<statements>;  
Else if(<Condition>)  
<statements>;  
---------------------  
-----------------------  
Else  
<statements>;  

Example

W.A.P to find the greatest number using an if statement:

using System;  
class ifdemo  
{  
  public static void Main()  
  {  
  int a,b;  
  Console.WriteLine("enter 2 no ");  
   a=Int.Parse (Console.ReadLine());  
  b=Int.Parse(Console.ReadLine());  
  if(a>b)  
     {  
       Console.WriteLine("a is greather");  
      }  
  else If(a< b)  
      {  
      Console.WriteLine("b is greather");  
      }  
  else   
     {  
    Console.WriteLine("both are Equals");  
     }  
Console.ReadLine();  
}

Switch Statement

The switch statement compares two logical expressions.

Syntax

Switch(<Expression>)  
{  
Case <Value> :  
<stmts>  
Break;  
-----------------------  
-------------------------  
------------------------  
Default :  
<stmts>  
Break;  

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.

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
  
namespace ConditionalStatementDemo  
{  
    class Switchdemo  
    {  
        int ch;  
        public void getdata()  
        {  
            Console.WriteLine("choose the following color");  
            ch = int.Parse(Console.ReadLine());  
            switch (ch)  
            {  
                case 1:  
                    Console.WriteLine("you choose Red");  
                    break;  
                case 2 :  
                     Console.WriteLine("you choose Green");  
                    break;  
                case 3:  
                    Console.WriteLine("you choose Pink");  
                    break;  
                default:  
                    Console.WriteLine("you cant choose correct color");  
                    break;
            }  
        }  
        public static void Main()  
        {  
            Switchdemo obj = new Switchdemo();  
            obj.getdata();  
            Console.ReadLine();
        }  
    }  
}

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

For Loop

Syntax

For(initializar;Conition;iterator)  
{  
< statement >  
}  

Example

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
  
namespace ConditionalStatementDemo  
{  
    class ForLoop  
    {  
         
        public void getdata()  
        {  
              
            for (int i = 0; i <= 50; i++)  
            {  
                Console.WriteLine(i);  
            }  
        }  
        public static void Main()  
        {  
            ForLoop f = new ForLoop();  
            f.getdata();  
            Console.ReadLine();  
        }  
    }  
} 

While Loop

Syntax

While(Condition)  
{  
< statement >  
}

Example

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
  
namespace ConditionalStatementDemo  
{  
    class WhileDemo  
    {  
        int x;  
        public void whiledemo()  
        {  
            while (x <= 50)  
            {  
                Console.WriteLine(x);  
                x++;  
            }  
        }  
        public static void Main()  
        {  
            WhileDemo obj = new WhileDemo();  
            obj.whiledemo();  
            Console.ReadLine();  
        }  
    }  
}

Do ... While Loop

Syntax

Do  
{  
< statement >  
}  
While(Condition)  

Example 

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
  
namespace ConditionalStatementDemo  
{  
    class DoWhileDemo  
    {  
        int x;  
        public void does()  
        {  
            do  
            {  
                Console.WriteLine(x);  
                x++;  
            }  
            while (x <= 50);  
        }
        public static void Main()  
        {  
            DoWhileDemo obj = new DoWhileDemo();  
            obj.does();  
            Console.ReadLine();  
        }  
    }  
}

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 number of executions occur. In the case of for and while the minimum number 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

Foreach(type var in coll/Arr)  
{  
< statement >;  
}  

Your feedback and suggestion are always welcome by me.


Recommended Free Ebook
Similar Articles