for(C# 参考)for (C# reference)

在指定的布尔表达式的计算结果为 true 时,for 语句会执行一条语句或一个语句块。The for statement executes a statement or a block of statements while a specified Boolean expression evaluates to true.

for 语句块中的任何点上,可以使用 break 语句中断循环,或者可以使用 continue 语句继续执行到循环中的下一次迭代。At any point within the for statement block, you can break out of the loop by using the break statement, or step to the next iteration in the loop by using the continue statement. 还可以使用 gotoreturnthrow 语句退出 for 循环。You also can exit a for loop by the goto, return, or throw statements.

for 语句的结构Structure of the for statement

for 语句定义初始化表达式、条件和迭代器部分 :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.

以下示例显示定义了所有部分的 forThe 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.

  • 以下列表中显示用逗号分隔的零个或多个语句表达式:Zero or more statement expressions from the following list, separated by commas:

    • 赋值语句assignment statement

    • 方法的调用invocation of a method

    • increment 表达式添加前缀或后缀,如 ++ii++prefix or postfix increment expression, such as ++i or i++

    • decrement 表达式添加前缀或后缀,如 --ii--prefix or postfix decrement expression, such as --i or i--

    • 通过使用 new 运算符来创建对象creation of an object by using the new operator

    • await 表达式await expression

上例中的“初始化表达式”部分声明和初始化本地循环变量 iThe 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. 如果“条件”部分不存在或者布尔表达式的计算结果为 true,则执行下一个循环迭代;否则退出循环 。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 iterator section contains zero or more of the following statement expressions, separated by commas:

  • 赋值语句assignment statement

  • 方法的调用invocation of a method

  • increment 表达式添加前缀或后缀,如 ++ii++prefix or postfix increment expression, such as ++i or i++

  • decrement 表达式添加前缀或后缀,如 --ii--prefix or postfix decrement expression, such as --i or i--

  • 通过使用 new 运算符来创建对象creation of an object by using the new operator

  • await 表达式await expression

上例中的“迭代器”部分增加本地循环变量 :The iterator section in the example above increments the local loop variable:

i++

示例Examples

下面的示例阐释了几种不太常见的 for 语句部分的使用情况:为“初始化表达式”部分中的外部循环变量赋值、同时在“初始化表达式”部分和“迭代器”部分中调用一种方法,以及更改迭代器部分中的两个变量的值 。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.
}

以下示例定义无限 for 循环:The following example defines the infinite for loop:

for ( ; ; )
{
    // Body of the loop.
}

C# 语言规范C# language specification

有关详细信息,请参阅 C# 语言规范中的 for 语句部分。For more information, see The for statement section of the C# language specification.

请参阅See also