Conditional
Statement :-
A block of code that
can executed based on a condition is known as
“Conditional Statment”.
There are 2 Type
1)Conditional Branching
2)Conditional Looping
2)Conditional Looping
1)Conditional Branching :-
This statement allows you to branch
your code depending on whethere certain condition were meet or not.
In c# there are 2 Conditional Branching
1) IF statement
2) Switch statement
1) IF Statement :-
The if
statement which allows you to test whethere a specefic condition meet or not.
Syntax :-
If(<Condition>)
<statements>;
Else if(<Condition>)
<statements>;
---------------------
-----------------------
Else
<statements>;
Eg.
W.A.P to find greather number using
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();
}
2) Swithc Statement :-
Switch
statement which allows you to compare an
expression with a number of differnet values..
Syntax :-
Switch(<Expression>)
{
Case <Value> :
<stmts>
Break;
-----------------------
-------------------------
------------------------
Default :
<stmts>
Break;
}
Note :-
In case of c# Language using break after
every case block is mandatory which should be used even of the default.
Eg.
W.A.P to choose color using 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();
}
}
}
2)Conditional Loops :-
C# provide 4
different loops that allows you to execute block of code repeatedly untill a
certain condition is meet those are
1) For Loop
2) While loop
3) Do_ _ While Loop
4) Foreach Loop
Each and every loop requires 3 things in
common.
1)Initialization :-
which sets starting point of the
loop
2) Condition :- which sets to the ending point of the
loop
3)Itertion :- which state you next level eighter
in forword or backword
Direction
Syntax :-
For(initializar;Conition;iterator)
{
<
statement >
}
E.g :-
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();
}
}
}
2)While Loop :-
Syntax :-
While(Condition)
{
<
statement >
}
E.g :-
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();
}
}
}
3)
Do _ _While Loop :-
Syntax :-
Do
{
<
statement >
}
While(Condition)
E.g :-
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
case of for and while loop from
the first executation there will be condition verfication.
But in case of do—while the condition is
verified ony of the first execuation ,so
minimum no of execuation icase of for and while will be zero where as it is 1 in case of do-while
loop.
4)Foreach Loop :-
It
is specially design for accessing the values of an array and collection.
Syntax :-
Foreach(type var in coll/Arr)
{
<
statement >;
}
if any problem occour frankly leave mail to me dotnetbyabhipatil@gmail.com or for more article visit my blog dotnetbyabhipatil.blogspot.in
your feedback and suggestion is always welcome for me.
ConversionConversion EmoticonEmoticon