for and foreach
Can any on explain what is difference between for and foreach
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.
Sanjay KumarPosted Dec 3, 2013, 1:53 AM
http://www.dotnetperls.com/for
http://www.dotnetperls.com/foreach
http://www.codeproject.com/Articles/6759/FOREACH-Vs-FOR-C
Sanjeeb LenkaPosted Dec 3, 2013, 1:51 AM
The for loop executes a statement or a block of statements repeatedly until a specified expression evaluates to false. there is need to specify the loop bounds( minimum or maximum).
int j = 0;
for (int i = 1; i <= 5; i++)
{
j = j + i ;
}
The foreach statement repeats a group of embedded statements for each element in an array or an object collection.you do not need to specify the loop bounds minimum or maximum.
int j = 0;
int[] tempArr = new int[] { 0, 1, 2, 3, 5, 8, 13 };
foreach (int i in tempArr )
{
j = j + i ;
}