Tutorials Navigation
[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:
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
Comments
Related Tutorials
- 01. Emulating Xbox 360 on PC for Running COD4 With Mods(3,492)
- 02. How to: Matrix Numbers | Batch File(1,903)
- 03. How to Password Protect Files on Windows(857)
- 04. How to play Socom 2/3/ and Combined Assault on PC(6,717)
- 05. Modern Warfare 2 Vac Ban Bypass Tutorial(6,133)
- 06. How to embed an image on TheTechGame(3,098)
- 07. [PC] NIOH 2 OTHER USER SAVE RESIGN(12,981)
- 08. Host bot lobbies! Full Tutorial!(11,243)
- 09. Unban yourself [Plutonium BO2](14,240)
- 10. Fall Guys - How to Change Your Name Color on Fall Guys(8,385)
- 11. Best Crosshair Settings for Valorant(6,525)
- 12. Othercide The Surgeon Boss Guide(2,539)
- 13. Othercide Remembrances Unlock Guide(4,460)
- 14. Othercide Beginners Tips and Tricks(2,708)
- 15. How to Fix Grounded Crashes, Loading Time, Low FPS and Other(4,845)
"[C#] For & For Each Loop" :: Login/Create an Account :: 0 comments