C-SHARP - 3.12 Loop For and Foreach
We are now on Visual Studio to put the for and foreach loops into practice in C#. The main difference between a for loop and a while loop is that the variable initialization, the condition and the increment are all written inside the parentheses of the for statement instead of being scattered before and inside the braces. This makes the loop more compact, easier to read and reduces the risk of falling into an infinite loop because everything is set up in one place.
To build the for loop, we type for and inside the parentheses we declare an int variable called counter initialised to 0, then a semicolon, then the condition counter < tabPrenom.Length, another semicolon and finally the increment counter++. Inside the braces we put our instruction to display the array values:
for (int counter = 0; counter < tabPrenom.Length; counter++)
{
Console.WriteLine(tabPrenom[counter]);
}
Running the program produces exactly the same output as the while version, but the code is clearer and shorter. The foreach loop is a bit different: it does not work with an index. You declare a variable of the same type as the elements of the collection, then use the in keyword followed by the collection you want to iterate over.
Foreach syntax
- Declare the element type and name (for example
string unPrenom). - Add
infollowed by the collection (tabPrenom). - Inside the braces, use the variable directly: it holds the current element at each iteration.
foreach (string unPrenom in tabPrenom)
{
Console.WriteLine(unPrenom);
}
On round one unPrenom takes the first array value, on round two the second, and so on. Launching the program shows all the first names. These loop constructs are simple once practised; the next video proposes an exercise to consolidate them.
Summary
This lesson explores two fundamental looping constructs in C#: the for loop and the foreach loop. The for loop compacts initialization, condition, and increment within parentheses, reducing the risk of infinite loops compared to while loops. The foreach loop simplifies iteration by automatically assigning each element to a variable, eliminating the need for manual index management.
Key points
- For loops use three components: initialization (int counter = 0), condition (counter < array.Length), and increment (counter++) all within parentheses
- For loops are more concise and safer than while loops because the counter logic is centralized and less prone to infinite loop errors
- Foreach loops automatically assign each element of an array to a variable (e.g., string name in nameArray) without requiring index tracking
- Foreach is ideal when you only need the values; for loops provide more control when you need the index position
FAQ
What is the key difference between a for loop and a while loop in C#?
The for loop combines initialization, condition, and increment all in the parentheses for cleaner syntax, whereas a while loop requires manual variable initialization before the loop and manual increment inside the block. This makes for loops less prone to infinite loops.
When should you use foreach instead of for in C#?
Use foreach when you need to iterate through all elements of a collection and don't need the index position. Foreach is simpler and more readable. Use for when you need to access the index or control iteration precisely.
How does foreach assign values in each iteration?
In foreach, the declared variable (e.g., string prénom) is automatically assigned the value of the current element from the collection at each iteration. You reference the variable directly in the loop body rather than accessing it by index.