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