for (C# Reference)
By using a for loop, you can run a statement or a block of statements repeatedly until a specified expression evaluates to false. This kind of loop is useful for iterating over arrays and for other applications in which you know in advance how many times you want the loop to iterate.
In the following example, the value of i is written to the console and incremented by 1 during each iteration of the loop.
The for statement in the previous example performs the following actions.
First, the initial value of variable i is established. This step happens only once, regardless of how many times the loop repeats. You can think of this initialization as happening outside the looping process.
To evaluate the condition (i <= 5), the value of i is compared to 5.
If i is less than or equal to 5, the condition evaluates to true, and the following actions occur.
The Console.WriteLine statement in the body of the loop displays the value of i.
The value of i is incremented by 1.
The loop returns to the start of step 2 to evaluate the condition again.
If i is greater than 5, the condition evaluates to false, and you exit the loop.
Note that, if the initial value of i is greater than 5, the body of the loop doesn't run even once.
Every for statement defines initializer, condition, and iterator sections. These sections usually determine how many times the loop iterates.
The sections serve the following purposes.
The initializer section sets the initial conditions. The statements in this section run only once, before you enter the loop. The section can contain only one of the following two options.
The declaration and initialization of a local loop variable, as the first example shows (int i = 1). The variable is local to the loop and can't be accessed from outside the loop.
Zero or more statement expressons from the following list, separated by commas.
The condition section contains a boolean expression that’s evaluated to determine whether the loop should exit or should run again.
The iterator section defines what happens after each iteration of the body of the loop. The iterator section contains zero or more of the following statement expressions, separated by commas:
The body of the loop consists of a statement, an empty statement, or a block of statements, which you create by enclosing zero or more statements in braces.
You can break out of a for loop by using the break keyword, or you can step to the next iteration by using the continue keyword. You also can exit any loop by using a goto, return, or throw statement.
The first example in this topic shows the most typical kind of for loop, which makes the following choices for the sections.
The initializer declares and initializes a local loop variable, i, that maintains a count of the iterations of the loop.
The condition checks the value of the loop variable against a known final value, 5.
The iterator section uses a postfix increment statement, i++, to tally each iteration of the loop.
The following example illustrates several less common choices: assigning a value to an external loop variable in the initializer section, invoking the Console.WriteLine method in both the initializer and the iterator sections, and changing the values of two variables in the iterator section.
All of the expressions that define a for statement are optional. For example, the following statement creates an infinite loop.
For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.