Read the syntax
A for loop has three parts: initialization runs once, the condition is checked before every iteration, and the update runs after each body execution. Semicolons separate the parts inside parentheses.
Use i < collection.Length for zero-based indexing. Nested loops repeat the inner loop for each outer iteration, while multiple loop variables can be updated together.
Syntax
for (initialization; condition; update) {\n // code\n}Examples
Basic for Loop
The classic counting loop with initialization, condition, and update.
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i); // 0, 1, 2, 3, 4
}- The classic counting loop with initialization, condition, and update.
- A for loop has three parts: initialization runs once, the condition is checked before every iteration, and the update runs after each body execution. Semicolons separate the parts inside parentheses.
- i starts at 0, increments after each print and stops when it reaches 5. The output is five lines containing 0 through 4.
Looping Over an Array by Index
Using a for loop when the index itself is needed, not just the value.
string[] names = { "John", "Zain", "Jamal" };
for (int i = 0; i < names.Length; i++)
{
Console.WriteLine($"{i}: {names[i]}");
}- Using a for loop when the index itself is needed, not just the value.
- A for loop has three parts: initialization runs once, the condition is checked before every iteration, and the update runs after each body execution. Semicolons separate the parts inside parentheses.
- i indexes the names array from 0 to Length - 1, so each printed line pairs a valid index with its name. The bound prevents an out-of-range access.
Nested for Loops
A loop inside another loop, commonly used for working with grids.
for (int row = 1; row <= 3; row++)
{
for (int col = 1; col <= 3; col++)
{
Console.Write(row * col + " ");
}
Console.WriteLine();
}- A loop inside another loop, commonly used for working with grids.
- A for loop has three parts: initialization runs once, the condition is checked before every iteration, and the update runs after each body execution. Semicolons separate the parts inside parentheses.
- For each row, the inner loop visits columns 1 through 3 and writes their products. The outer WriteLine ends each row, making a 3-by-3 multiplication grid.
for Loop with Multiple Variables
C#'s for loop can initialize and update more than one variable at once.
for (int i = 0, j = 10; i < j; i++, j--)
{
Console.WriteLine($"i={i}, j={j}");
}- C#'s for loop can initialize and update more than one variable at once.
- A for loop has three parts: initialization runs once, the condition is checked before every iteration, and the update runs after each body execution. Semicolons separate the parts inside parentheses.
- i increases while j decreases; the condition i < j is rechecked each time. The printed pairs are i=0, j=10 through i=4, j=6.
Best practices
- Use foreach whenever you only need each element, not its index - it is safer and more readable than a for loop
- Use a traditional for loop when you need the index, need to iterate backward, or need to skip elements with a custom step
- Avoid modifying the loop counter variable inside the loop body beyond the update expression - it creates subtle, hard-to-follow bugs
- Prefer for over while when the number of iterations is known in advance, since it groups all loop control in one place
Runtime & versions
.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
C# language reference