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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Apr 11, 2012, 10:08 AM EDT
http://msdn.microsoft.com/en-us/library/ch45axte.aspx
http://msdn.microsoft.com/en-us/library/ttw7t8t6.aspx
SenthilkumarRank #387 · 6.5K reputationPosted Apr 11, 2012, 7:45 PM EDT
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.
Satyapriya NayakRank #47 · 31.5K reputationPosted Apr 11, 2012, 8:15 PM EDT
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