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:
- Conditional Branching
- 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:
- IF statement
- 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.
- Initialization: that sets a starting point of the loop
- Condition: that sets an ending point of the loop
- 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.


Mukesh KumarPosted Oct 16, 2015, 4:01 AM EDT
Thanks Sir
Rahul JhaPosted Oct 16, 2015, 3:36 AM EDT
Good one
Suman VermaPosted Oct 16, 2015, 1:42 AM EDT
Nice job mukesh
Yaduveer SainiPosted Oct 16, 2015, 1:37 AM EDT
Good Job Sir...keep it up
Mukesh KumarPosted Oct 10, 2015, 12:58 AM EDT
Thanks sir
Akash VarshneyPosted Oct 10, 2015, 12:22 AM EDT
good one
Mukesh KumarPosted Oct 5, 2015, 9:43 PM EDT
Thanks sir
Santhakumar MunuswamyPosted Sep 30, 2015, 11:49 AM EDT
Welcome
Santhakumar MunuswamyPosted Sep 30, 2015, 11:49 AM EDT
Good Start
Mukesh KumarPosted Sep 30, 2015, 4:43 AM EDT
Thanks
Karthikeyan KPosted Sep 30, 2015, 12:15 AM EDT
Good one...Thanks for sharing
Mukesh KumarPosted Sep 29, 2015, 11:23 PM EDT
Thanks Sibeesh
Sibeesh VenuTOP 100Posted Sep 29, 2015, 10:25 PM EDT
Nice Share
Mohammed IbrahimPosted Sep 29, 2015, 11:31 AM EDT
nice
Mukesh KumarPosted Sep 29, 2015, 9:17 AM EDT
Thank you Raja..
Raja TPosted Sep 29, 2015, 8:52 AM EDT
Good , Thanks for sharing :)
Bruno PétersonPosted Sep 29, 2015, 8:42 AM EDT
Cool
Mukesh KumarPosted Sep 29, 2015, 5:05 AM EDT
Thanks Sir
Pankaj Kumar ChoudharyTOP 100Posted Sep 29, 2015, 4:57 AM EDT
Nice Start Mukesh Welcome to C#-Corner Community .......
Shakti SaxenaPosted Sep 29, 2015, 4:28 AM EDT
nice
Mukesh KumarPosted Sep 29, 2015, 3:41 AM EDT
Thank you so much Rakesh and Ankit...
Ankit BansalPosted Sep 29, 2015, 3:24 AM EDT
nice...
RakeshPosted Sep 29, 2015, 3:12 AM EDT
Good start :)
Mukesh KumarPosted Sep 29, 2015, 3:12 AM EDT
Thanks Humayun..
Humayun Kabir MamunPosted Sep 29, 2015, 3:03 AM EDT
Nice...
Mukesh KumarPosted Sep 29, 2015, 2:52 AM EDT
Thanks Sujeet to motivate me.
Sujeet SumanPosted Sep 29, 2015, 2:19 AM EDT
Good one..............................
Mukesh KumarPosted Sep 29, 2015, 2:15 AM EDT
Thanks Rahul Rathor and Manas Mohapatra.
Manas MohapatraPosted Sep 29, 2015, 2:06 AM EDT
Very good Start..
rahul rathorePosted Sep 29, 2015, 2:03 AM EDT
g8 article mukesh
Mukesh KumarPosted Sep 29, 2015, 1:56 AM EDT
Thanks Yudi and Harshad to motivate me... I will try to continue posting some new articles\
Atul RawatPosted Sep 29, 2015, 1:55 AM EDT
nice article... good start
Harshad PansuriyaPosted Sep 29, 2015, 1:53 AM EDT
Nice Share
Yaduveer SainiPosted Sep 29, 2015, 1:51 AM EDT
Hi Mukesh, It's very nice article which is helpful for me.