The For Statement

 

SHORT DESCRIPTION

A language command for running a command block based on a conditional test

 

LONG DESCRIPTION

The for statement (also known as a for loop) is a language construct for creating a loop that runs commands in a command block while a specified condition evaluates to true.

 

A typical use of the for loop is to iterate an array of values and operate on a subset of these values. In most cases, if you want to iterate all values in an array, consider using a foreach statement.

 

The following shows the for statement syntax:

 

for (<init>; <condition>; <repeat>)

{<command_block>}

 

·         <init> is one or more commands, separated by commas, that are run before the loop begins. You will commonly use the init portion of the statement to create and initialize a variable with a starting value. This variable will then be the basis for the condition to be tested in the next portion of the for statement.

·         <condition> is the portion of the for statement that resolves to a Boolean true or false value. PowerShell evaluates the condition each time the for loop runs. If the statement is true, the commands in the <command_block> run, and the statement is evaluated again. If the condition is still true, the <command_block> commands run again, and so on, until the condition becomes false.

·         <repeat> is 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.

·         <command_block> is a set of one or more commands that are run each time the loop is entered or repeated. The contents of the command_block are surrounded by braces.

 

At a minimum, a for statement requires the parenthesis surrounding the init, condition, and repeat portion of the statement and a command surrounded by braces in the command_block portion of the statement.

 

Note that the upcoming examples intentionally show code outside of the for statement. In later examples, code will be integrated into the for statement.

 

For example, the following for statement will continually display the value of the $i variable until you manually break out of the command by pressing  Ctrl+C.

 

$i = 1

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

 

You can add additional commands to the command_block so, for example, the value of $i is incremented by 1 each time through the loop, as the following example shows:

 

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

 

Until you break out of the command by pressing Ctrl+C, this statement will continually display the value of the $i variable as it gets incremented by 1 each time through the loop.

 

Rather than change the value of the variable in the command_block portion of the for statement, you can use the repeat portion of the for statement instead.

 

$i=1

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

 

This will still repeat indefinitely or until you break out of the command by pressing Ctrl+C.

 

By setting a condition (using the condition portion of the for statement), you can end the for loop when the condition evaluates to false. 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}

 

Instead of creating and initializing the variable outside of the for statement, you can perform this task inside of the for loop by using the init portion of the for statement.

 

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

 

You can use carriage returns (instead of semi-colons) to delimit the init, condition, and repeat portion in the for statement. The following shows the for statement syntax in this alternative form:

 

for (<init>

<condition>

<repeat>){

<command_block>

}

 

This alternative form of the for statement works in PowerShell script files and at the PowerShell command prompt. However, it is easier to use the for statement syntax with semi-colons when entering interactive commands at the console.

 

The for loop is more flexible than the foreach loop by allowing you to increment values in an array or collection by using patterns. In the following example, the $i variable is incremented by 2 in the repeat portion of the for statement:

 

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

 

SEE ALSO 

 

For information about performing boolean tests, enter the following command at the PowerShell command prompt:

 

help about_comparison_operators

 

For information about the foreach statement, enter the following command at the PowerShell command prompt:

 

help about_foreach