Control statements give you additional means to control the
processing within the applications you develop. These conditions are specified
by a set of decision-making statements having Boolean expressions that are
evaluated to a Boolean value true or false.
Comparison
Operators
Operator
|
Action
|
==
|
Equal to
|
!=
|
Not equal to
|
>
|
Greater than
|
>=
|
Greater than or equal to
|
<
|
Less than
|
Types
of Control statements in C#
1. IF statement
2. Switch statement
3. For Loop
4. While loop
5. Do ... While Loop
1 IF statement
C# if statement will execute a block of code if the given
condition is true.
Syntax:
if(statement is TRUE)
EXECUTE this line of code
Syntax Example 1
if(expression)
{
statements;
}
else
{
statements;
}
|
Example
1
class Program
{
static void Main(string[] args)
{
int age = 0;
Console.WriteLine(“please enter your age
to find out your eligibility for voting”) ;
age =
Convert.ToInt32(Console.ReadLine());
if (age < 18)
{
Console.WriteLine(“You are not eligible
for voting”) ;
}
else
{
Console.WriteLine(“You are eligible for voting”) ;
}
Console.Read();
}
}
|
2
- Switch statement
The switch statement is like a set of if statements. It's a
list of possibilities, with an action for each possibility, and an optional the default action, in case nothing else evaluates to true, so we can say that A
switch statement allows a variable to be tested for equality against a list of
values.
Syntax:
-
switch ( CaseVariable )
{
case constantExpression_1:
statements;
break;
case constantExpression_2:
statements;
break;
case constantExpression_3:
statements;
break;
default:
statements;
break;
}
|
Example
of switch statement
public class switchStatementExample
{
static void Main(string[] args)
{
char grade = 'B';
switch (grade)
{
case 'A':
Console.WriteLine("Excellent!");
break;
case 'B':
case 'C':
Console.WriteLine("Well done");
break;
case 'D':
Console.WriteLine("You passed");
break;
case 'F':
Console.WriteLine("Better try
again");
break;
default:
Console.WriteLine("You Failed!");
break;
}
}
}
|
While
loop
The while
loop includes a Boolean expression as a condition that will return true or
false. It executes the code block, as long as the specified conditional
expression returns true
Syntax:-
while ( expression )
{
statements;
}
|
Example of while loop
class whileLoopExampleProgram
{
static void Main(string[] args)
{
int i;
i = 1;
while (i < 50)
{
Console.WriteLine(“current Value of the
i variable is :”+i);
i = i + 5;
}
Console.Read();
}
}
|
For
Loop
The for loop contains three parts: initialization,
conditional expression and steps, which are separated by a semicolon,
1- variable initialization:
Declare & initialize a variable here which will be used in conditional
expression and steps part.
2- condition: The
condition is a boolean expression that will return either true or false.
3- steps: The steps
defines the incremental or decremental part
Syntax
for ( initialization;
termination; increment/decrement)
{
statements
}
|
For
Loop example
class ForLoopExampleProgram
{
static void Main(string[] args)
{
int i;
i = 1;
for (i = 0; i <= 50; i++)
{
Console.WriteLine("current Value of
the i variable is :" + i.ToString());
i = i + 5;
}
Console.Read();
}
|
Do-while
loop
The do-while construct consists of a process symbol and a
condition. First, the code within the block is executed, and then the condition
is evaluated. If the condition is true the code within the block is executed
again.
Syntax
of do-While loop
do
{
statements;
} while ( expression );
|
Example
of do-while loop:-
namespace ConsoleApplication1
{
class do_while_LoopExampleProgram
{
static void Main(string[] args)
{
int i = 0;
i = 60;
do
{
Console.WriteLine("value of i is- " + i.ToString());
i = i + 60;
} while (i < 20000);
Console.Read();
}
}
}
|