Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Keywords and Statements

The for statement

Reference for the C# for loop: initializer, condition and iterator sections, multiple variables, counting backwards, infinite loops and closure capture.

The for statement repeats a body while a condition is true. Its header has three optional parts separated by semicolons: an initializer that runs once, a condition checked before every iteration, and an iterator that runs after every iteration. It is the natural choice when you need an index or a fixed number of repetitions.

Variables declared in the initializer are scoped to the loop. If you only need the elements of a collection and not their positions, foreach is shorter and cannot go out of range.

C#
for (int i = 0; i < 5; i++)
{
    Console.WriteLine(i);
}

Try it

C# Counting up, down and in steps

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Omitted sections and a closure trap

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
for ([initializer]; [condition]; [iterator])
    statement
Name Description
initializer A local variable declaration (one type, several variables allowed) or a comma-separated list of statement expressions. Runs once.
condition A bool expression evaluated before each iteration. If omitted, it is true.
iterator One or more comma-separated statement expressions, run after each iteration.
statement The body: a single statement or a block.

Errors: a non-bool condition fails with CS0029. Modifying a variable you did not declare in the header is fine, but assigning to the loop variable of a foreach is not (CS1656), which is one reason to prefer for when you need to change the index.

Usage notes

  • Order of execution: initializer, then condition, body, iterator, condition, body, iterator, until the condition is false. continue jumps to the iterator; break leaves the loop.
  • Off-by-one errors are the classic bug. Use < array.Length for zero-based indexing; <= runs once too often and throws IndexOutOfRangeException.
  • Closures capture the variable, not its value. Unlike foreach (since C# 5), the for variable is a single variable for the whole loop, as shown above.
  • Floating-point counters accumulate rounding error: for (double x = 0; x != 1; x += 0.1) never ends. Use an integer counter and compute the value.
  • Modifying the collection while looping by index is allowed (unlike foreach over a List<T>, which throws InvalidOperationException), but be careful with shifting indices.
  • Performance: the JIT removes bounds checks for the pattern for (int i = 0; i < array.Length; i++). Reading list.Count or array.Length each time is cheap.
  • Infinite loops: for (;;) and while (true) are equivalent; leave with break, return or an exception.