1. For loop
its used to run a statement or a block of statements repeatedly until a specified expression evaluates to false.
Syntax :
for (<initialization>;<condition check>;<increment/decrement>)
{
//block to execute when the condition holds true
}
Example
for (int i=0 ; i < 5 ; i++) //intialization;condition;increament/decrement
{
Console.WriteLine(number);
}
output :
0
1
2
3
4
2. For each loop
'for each' loop is used to loop through an array of object or collections. it will automatically come out of the execution block when it has visited all the items in the array.
Syntax
foreach(<DataType> <VarName> in <ListName>)
{
//use VarName and perform operation
}
Example :
ArrayList listName = new ArrayList();
listName.Add("avinash");
listName.Add("nigel");
listName.Add("miten");
//will loop through each item inside the array list
foreach(string name in listName)
{
Console.WriteLine(name);
}
output :
Avinash
Nigel
Miten
3. While loop
The while statement executes a statement or a block of statements until a specified expression evaluates to false.
Syntax :
while(condition)
{
//evaluate the condition and execute the block till the condition holds true
}
Example :
int number = 0;
//do-while evaluates the expression before the block of code is executed
while(number < 5)
{
Console.WriteLine(number);
number++;
}
output :
0
1
2
3
4
Do While Loop
do statement executes a statement or a block of statements repeatedly until a specified expression evaluates to false.
Syntax :
do
{
//run the block for 1st time
//next iteration evaluate the condition and then execute the block if the condition holds true.
}
while (condition);
example :
int numberDo = 0;
do
{
//this block will run at least once even if the evaluation returns false at 1st check.
Console.WriteLine(number);
numberDo++;
//do-while evaluates the expression after the block of code is executed
}
while (number < 5);
output :
0
1
2
3
4
its used to run a statement or a block of statements repeatedly until a specified expression evaluates to false.
Syntax :
for (<initialization>;<condition check>;<increment/decrement>)
{
//block to execute when the condition holds true
}
Example
for (int i=0 ; i < 5 ; i++) //intialization;condition;increament/decrement
{
Console.WriteLine(number);
}
output :
0
1
2
3
4
2. For each loop
'for each' loop is used to loop through an array of object or collections. it will automatically come out of the execution block when it has visited all the items in the array.
Syntax
foreach(<DataType> <VarName> in <ListName>)
{
//use VarName and perform operation
}
Example :
ArrayList listName = new ArrayList();
listName.Add("avinash");
listName.Add("nigel");
listName.Add("miten");
//will loop through each item inside the array list
foreach(string name in listName)
{
Console.WriteLine(name);
}
output :
Avinash
Nigel
Miten
3. While loop
The while statement executes a statement or a block of statements until a specified expression evaluates to false.
Syntax :
while(condition)
{
//evaluate the condition and execute the block till the condition holds true
}
Example :
int number = 0;
//do-while evaluates the expression before the block of code is executed
while(number < 5)
{
Console.WriteLine(number);
number++;
}
output :
0
1
2
3
4
Do While Loop
do statement executes a statement or a block of statements repeatedly until a specified expression evaluates to false.
Syntax :
do
{
//run the block for 1st time
//next iteration evaluate the condition and then execute the block if the condition holds true.
}
while (condition);
example :
int numberDo = 0;
do
{
//this block will run at least once even if the evaluation returns false at 1st check.
Console.WriteLine(number);
numberDo++;
//do-while evaluates the expression after the block of code is executed
}
while (number < 5);
output :
0
1
2
3
4
No comments:
Post a Comment