Codectionary / Developer documentation / C#

for Loop

The for loop repeats a block of code a specific number of times, controlled by three parts in its header: an initialization (run once), a condition (checked before every iteration), and an update (run after every iteration). It's the standard choice when the number of iterations is known or easily computed in advance, such as looping over an index range.

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
}
  1. The classic counting loop with initialization, condition, and update.
  2. 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.
  3. 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]}");
}
  1. Using a for loop when the index itself is needed, not just the value.
  2. 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.
  3. 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();
}
  1. A loop inside another loop, commonly used for working with grids.
  2. 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.
  3. 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}");
}
  1. C#'s for loop can initialize and update more than one variable at once.
  2. 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.
  3. 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

Specifications & further reading