关于About For

简短说明Short description

描述可用于根据条件测试运行语句的语言命令。Describes a language command you can use to run statements based on a conditional test.

长说明Long description

For语句 (也称为 For 循环) 是一种语言构造,可用于创建在指定条件的计算结果为时在命令块中运行命令的循环 $trueThe For statement (also known as a For loop) is a language construct you can use to create a loop that runs commands in a command block while a specified condition evaluates to $true.

循环的典型用途 For 是循环访问值的数组,并对这些值的子集进行运算。A typical use of the For loop is to iterate an array of values and to operate on a subset of these values. 在大多数情况下,如果要对数组中的所有值进行迭代,请考虑使用 Foreach 语句。In most cases, if you want to iterate all the values in an array, consider using a Foreach statement.

语法Syntax

下面显示了 For 语句语法。The following shows the For statement syntax.

for (<Init>; <Condition>; <Repeat>)
{
    <Statement list>
}

Init 占位符表示在循环开始之前运行的一个或多个命令。The Init placeholder represents one or more commands that are run before the loop begins. 通常使用语句的 Init 部分来创建和初始化具有起始值的变量。You typically use the Init portion of the statement to create and initialize a variable with a starting value.

然后,此变量将成为该语句的下一部分中要测试的条件的基础 ForThis variable will then be the basis for the condition to be tested in the next portion of the For statement.

条件 占位符表示 For 语句中解析为 $true$false 布尔 值的部分。The Condition placeholder represents the portion of the For statement that resolves to a $true or $false Boolean value. PowerShell 将在每次循环运行时评估条件 ForPowerShell evaluates the condition each time the For loop runs. 如果语句为 $true ,则命令块中的命令将运行,并再次计算语句。If the statement is $true, the commands in the command block run, and the statement is evaluated again. 如果条件仍为 $true语句列表 中的命令将再次运行。If the condition is still $true, the commands in the Statement list run again. 循环将重复,直到该条件变为 $falseThe loop is repeated until the condition becomes $false.

重复 占位符表示一个或多个以逗号分隔的命令,这些命令在每次循环重复时执行。The Repeat placeholder represents one or more commands, separated by commas, that are executed each time the loop repeats. 通常,这用于修改在语句的 条件 部分内测试的变量。Typically, this is used to modify a variable that is tested inside the Condition part of the statement.

语句列表 占位符表示在每次输入或重复循环时运行的一个或多个命令集。The Statement list placeholder represents a set of one or more commands that are run each time the loop is entered or repeated. 语句列表 的内容括在大括号中。The contents of the Statement list are surrounded by braces.

支持多个操作Support for multiple operations

Init 语句中的多个赋值操作支持以下语法:The following syntaxes are supported for multiple assignment operations in the Init statement:

# Comma separated assignment expressions enclosed in parenthesis.
for (($i = 0), ($j = 0); $i -lt 10; $i++)
{
    "`$i:$i"
    "`$j:$j"
}

# Sub-expression using the semicolon to separate statements.
for ($($i = 0;$j = 0); $i -lt 10; $i++)
{
    "`$i:$i"
    "`$j:$j"
}

重复 语句中的多个赋值操作支持以下语法:The following syntaxes are supported for multiple assignment operations in the Repeat statement:

# Comma separated assignment expressions.
for (($i = 0), ($j = 0); $i -lt 10; $i++)
{
    "`$i:$i"
    "`$j:$j"
}

# Comma separated assignment expressions enclosed in parenthesis.
for (($i = 0), ($j = 0); $i -lt 10; ($i++), ($j++))
{
    "`$i:$i"
    "`$j:$j"
}

# Sub-expression using the semicolon to separate statements.
for ($($i = 0;$j = 0); $i -lt 10; $($i++;$j++))
{
    "`$i:$i"
    "`$j:$j"
}

备注

除 pre 或 post 以外的其他操作可能不适用于所有语法。Operations other than pre or post increment may not work with all syntaxes.

对于多个 条件 使用逻辑运算符,如下面的示例所示。For multiple Conditions use logical operators as demonstrated by the following example.

for (($i = 0), ($j = 0); $i -lt 10 -and $j -lt 10; $i++,$j++)
{
    "`$i:$i"
    "`$j:$j"
}

有关详细信息,请参阅 about_Logical_OperatorsFor more information, see about_Logical_Operators.

示例Examples

至少,语句要求在语句的 For 语句列表部分中,语句的 InitConditionRepeat 部分以及括在语句的 语句列表 部分中的命令使用括号。At a minimum, a For statement requires the parenthesis surrounding the Init , Condition , and Repeat part of the statement and a command surrounded by braces in the Statement list part of the statement.

请注意,即将出现的示例有意显示语句外部的代码 ForNote that the upcoming examples intentionally show code outside the For statement. 在后面的示例中,代码集成到 For 语句中。In later examples, code is integrated into the For statement.

例如,下面的 For 语句持续显示变量的值, $i 直到你通过按 CTRL + C 手动跳出命令。For example, the following For statement continually displays the value of the $i variable until you manually break out of the command by pressing CTRL+C.

$i = 1
for (;;)
{
    Write-Host $i
}

您可以向语句列表中添加其他命令 $i ,以便每次运行该循环时,的值递增1,如下面的示例所示。You can add additional commands to the statement list so that the value of $i is incremented by 1 each time the loop is run, as the following example shows.

for (;;)
{
    $i++; Write-Host $i
}

在您通过按 CTRL + C 跳出命令之前,此语句将持续显示变量的值, $i 因为每次运行该循环时,它将递增1。Until you break out of the command by pressing CTRL+C, this statement will continually display the value of the $i variable as it is incremented by 1 each time the loop is run.

For您可以改为使用语句的 重复 部分,而不是在语句的语句列表部分中更改变量的值, For 如下所示。Rather than change the value of the variable in the statement list part of the For statement, you can use the Repeat portion of the For statement instead, as follows.

$i=1
for (;;$i++)
{
    Write-Host $i
}

此语句仍会无限重复,直到你通过按 CTRL + C 退出命令。This statement will still repeat indefinitely until you break out of the command by pressing CTRL+C.

您可以 For 使用 条件 终止循环。You can terminate the For loop using a condition . 您可以使用语句的 条件 部分来放置条件 ForYou can place a condition using the Condition portion of the For statement. For当条件的计算结果为时,循环将终止 $falseThe For loop terminates when the condition evaluates to $false.

在下面的示例中, For 当的值 $i 小于或等于10时,将运行循环。In the following example, the For loop runs while the value of $i is less than or equal to 10.

$i=1
for(;$i -le 10;$i++)
{
    Write-Host $i
}

For您可以 For 使用语句的 Init 部分在循环内执行此任务,而不是在语句外部创建和初始化变量 ForInstead of creating and initializing the variable outside the For statement, you can perform this task inside the For loop by using the Init portion of the For statement.

for($i=1; $i -le 10; $i++){Write-Host $i}

您可以使用回车符而不是分号来分隔语句的 InitConditionRepeat 部分 ForYou can use carriage returns instead of semicolons to delimit the Init , Condition , and Repeat portions of the For statement. 下面的示例演示一个 For 使用此替代语法的。The following example shows a For that uses this alternative syntax.

for ($i = 0
  $i -lt 10
  $i++){
  $i
}

该语句的这种替代形式 For 适用于 powershell 脚本文件和 powershell 命令提示符。This alternative form of the For statement works in PowerShell script files and at the PowerShell command prompt. 但是,在 For 命令提示符下输入交互命令时,使用带有分号的语句语法会更容易。However, it is easier to use the For statement syntax with semicolons when you enter interactive commands at the command prompt.

For循环比循环更灵活, Foreach 因为它允许您通过使用模式来递增数组或集合中的值。The For loop is more flexible than the Foreach loop because it allows you to increment values in an array or collection by using patterns. 在下面的示例中,在 $i 语句的 重复 部分,变量递增 2 ForIn the following example, the $i variable is incremented by 2 in the Repeat portion of the For statement.

for ($i = 0; $i -le 20; $i += 2)
{
    Write-Host $i
}

For还可以在一行上写入循环,如以下示例中所示。The For loop can also be written on one line as in the following example.

for ($i = 0; $i -lt 10; $i++) { Write-Host $i }

另请参阅SEE ALSO

about_Comparison_Operatorsabout_Comparison_Operators

about_Foreachabout_Foreach