for
The for
statement executes a statement or a block of statements while a specified Boolean expression evaluates to true
.
At any point within the for
statement block, you can break out of the loop by using the breakstatement, or step to the next iteration in the loop by using the continue statement. You also can exit a for
loop by the goto, return, or throw statements.
Structure of the for
statement
The for
statement defines initializer, condition, and iterator sections:
for (initializer; condition; iterator)
body
All three sections are optional. The body of the loop is either a statement or a block of statements.
The following example shows the for
statement with all of the sections defined:
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
The initializer section
The statements in the initializer section are executed only once, before entering the loop. The initializer section is either of the following:The declaration and initialization of a local loop variable, which can't be accessed from outside the loop.
-
The declaration and initialization of a local loop variable, which can't be accessed from outside the loop.
-
Zero or more statement expressions from the following list, separated by commas:
-
assignment statement
-
invocation of a method
-
prefix or postfix increment expression, such as
++i
ori++
-
prefix or postfix decrement expression, such as
--i
ori--
-
creation of an object by using new keyword
-
await expression
-
The initializer section in the example above declares and initializes the local loop variable i
:
int i = 0
The condition section
The condition section, if present, must be a boolean expression. That expression is evaluated before every loop iteration. If the condition section is not present or the boolean expression evaluates to true
, the next loop iteration is executed; otherwise, the loop is exited.
The condition section in the example above determines if the loop terminates based on the value of the local loop variable:
i < 5
The iterator section
The iterator section defines what happens after each iteration of the body of the loop. The iteratorsection contains zero or more of the following statement expressions, separated by commas:
-
assignment statement
-
invocation of a method
-
prefix or postfix increment expression, such as
++i
ori++
-
prefix or postfix decrement expression, such as
--i
ori--
-
creation of an object by using new keyword
-
await expression
The iterator section in the example above increments the local loop variable:
i++
Examples
The following example illustrates several less common usages of the for
statement sections: assigning a value to an external loop variable in the initializer section, invoking a method in both the initializer and the iterator sections, and changing the values of two variables in the iterator section. Select Run to run the example code. After that you can modify the code and run it again.
int i;
int j = 10;
for (i = 0, Console.WriteLine($"Start: i={i}, j={j}"); i < j; i++, j--, Console.WriteLine($"Step: i={i}, j={j}"))
{
// Body of the loop.
}
The following example defines the infinite for
loop:
for ( ; ; )
{
// Body of the loop.
}