Tutorials Navigation

Tutorials :: New :: Popular :: Top Rated

Tutorials: 18,357 Categories: 12

Total Tutorial Views: 42,397,194

[C#] For & For Each Loop

Tutorial Name: [C#] For & For Each Loop  

Category: PC Tutorials

Submitted By: Skittle

Date Added:

Comments: 0

Views: 1,499

Related Forum: PC Building Forum

Share:

In this tutorial I will show you how to use the for and foreach loops in your programs. First, create your project and locate to where you want to use these loops. Loops can be used to step through elements of an Array, Collection, or just going up a number each time.

for loop

The for loop is used to loop through a group of numbers incrementing each time. It could go from 0 to 6, 11 to 100, anything like that. Here is how you use these loops to step through an array:

//creating an array with 3 elements
string[] strArr = new string[3] {"A", "B", "C"};
//declaring a string to hold final data
string final = null;
for(int i = 0; i < 3; i++)
{
    //adds array value to final string
    final += strArr[i];
}
//prints out final string
MessageBox.Show(final);

This is the structure of a for loop:

for(number variable that goes up, keep looping while i is less than 3, increment i by one each time we loop)
When we run this, here is the output:
[ Register or Signin to view external links. ]

To try and explain this better, I will show you what happens on each loop:
    Loop 1:
  • i = 0
  • strArr[i] = "A"
  • final = "A"

    Loop 2:
  • i = 1
  • strArr[i] = "B"
  • final = "AB"

    Loop 3:
  • i = 2
  • strArr[i] = "C"
  • final = "ABC"

We do not loop again because i = 3, so when it is checked if i < 3, the answer is false because i is equal to 3, not less than.

foreach loop

The foreach loop is used to step through a collection, such as a HtmlElementCollection or an Array. Instead of incrementing an integer variable, every time it loops you are passed the next element of the collection, so if you are stepping through a HtmlElementCollection then you will be given the next HtmlElement in the Collection, if you are stepping through a string Array then you are given the next string in the Collection etc. Here is how you would use a foreach loop to get the same output as you would in the above for loop example:

foreach(string str in strArr)
{
    //adds current element of collection to final string
    final += str;
}
//prints our final string
MessageBox.Show(final);

This will give you exactly the same result as the example above, printing out "ABC". Once again, I will show you what the variables hold each time it loops:
    Loop 1:
  • strArr = {"A", "B", "C"}
  • str = "A"
  • final = "A"

    Loop 2:
  • strArr = {"A", "B", "C"}
  • str = "B"
  • final = "AB"

    Loop 3:
  • strArr = {"A", "B", "C"}
  • str = "C"
  • final = "ABC"


foreach loops will loop through until there are no more elements in the collection, not if the current element is null.

If you found this tutorial helpful, any REP is appreciated
If you nee dany help, feel free to PM me

Ratings

Please take one second and rate this tutorial...

Not a Chance
1
2
3
4
5
6
7
8
9
10
Absolutely

Comments

"[C#] For & For Each Loop" :: Login/Create an Account :: 0 comments

If you would like to post a comment please signin to your account or register for an account.