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.
for (int i = 0; i < 5; i++) { Console.WriteLine(i); }
Try it
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.
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
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.
continuejumps to the iterator;breakleaves the loop. - Off-by-one errors are the classic bug. Use
< array.Lengthfor zero-based indexing;<=runs once too often and throwsIndexOutOfRangeException. - Closures capture the variable, not its value. Unlike
foreach(since C# 5), theforvariable 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
foreachover aList<T>, which throwsInvalidOperationException), but be careful with shifting indices. - Performance: the JIT removes bounds checks for the pattern
for (int i = 0; i < array.Length; i++). Readinglist.Countorarray.Lengtheach time is cheap. - Infinite loops:
for (;;)andwhile (true)are equivalent; leave withbreak,returnor an exception.