Please Answere me.
what is for and foreach loop and explain simple examples?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Apr 12, 2012, 12:15 AM
for loop
The for loop executes a statement or a block of statements repeatedly until a specified expression evaluates to false. The for loop is useful for iterating over arrays and for sequential processing
Because the test of a conditional expression occurs before the execution of the loop, a for statement executes zero or more times.
Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace For_foreach_loop
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 5; i++)
{
Console.WriteLine(i);
}
Console.ReadLine();
}
}
}
For Each loop is used to iterate through arrays or collections.
foreach loop
The foreach statement repeats a group of embedded statements for each element in an array or an object collection. The foreach statement is used to iterate through the collection to get the desired information, but should not be used to change the contents of the collection to avoid unpredictable side effects.
Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace For_foreach_loop
{
class Program
{
static void Main(string[] args)
{
int[] arr1 = new int[] { 0, 1, 2, 3, 4, 5, 6 };
foreach (int i in arr1)
{
Console.WriteLine(i);
}
Console.ReadLine();
}
}
}
Thanks
SenthilkumarPosted Apr 11, 2012, 11:45 PM
The for and foreach is used to iterate.
For loop:
The for loop has the option to set the initial value and seed. It has the three parts.
Initialization
Condition
Increment/Decrement
Syntax:
for(initialization;condition;step value)
{
//code here.
}
Suppose we write the code to print 10 values.
for(int i=0;i<10;i++)
{
Console.WriteLine(i.ToString());
}
It starts from 0 and it will print upto 9.
Foreach:
he foreach statement repeats a group of embedded statements for each element in an array or an object collection. The foreach statement is used to iterate through the collection to get the desired information, but should not be used to change the contents of the collection to avoid unpredictable side effects.
// Use a string array to loop over.
string[] ferns =
{
"Psilotopsida",
"Equisetopsida",
"Marattiopsida",
"Polypodiopsida"
};
// Loop with the foreach keyword.
foreach (string value in ferns)
{
Console.WriteLine(value);
}
It will fetch the each item from the object and print of the same data type. It has the inbuilt cursor to maintain the row by row activities.
Differences:
There are some differences.
1) for loop needs to set manually initialize and step value, but the for each can read row by row automatically
2) for loop can increment the step (i+=2), it will increment the row by two. But foreach has t be read row by row and can't jump
3) for loop can be set to run indefinitely. But the foreach can't.
for(;;)
{
//infinite loop.
}
Hope this will help you.
VulpesPosted Apr 11, 2012, 2:08 PM
http://msdn.microsoft.com/en-us/library/ch45axte.aspx
http://msdn.microsoft.com/en-us/library/ttw7t8t6.aspx