for

The for loop is typically used to step through a collection using the following notation:

for (<intial>; <exit condition>; <repeat>){ 
    <body-statements>
}

<initial> represents the state of a variable before the first iteration of the loop. This is normally used to initialize a counter for the loop.

The exit condition must be true as long as the loop is executing.

<repeat> is executed after each iteration of the body and is often used to increment a counter.

The for loop is most often used to iterate through a collection, for example:

$processes = Get-Process 
for ($i = 0; $i -lt $processes.Count; $i++) { 
    Write-Host $processes[$i].Name
}

The for loop provides a significant degree of control over the loop and is useful where the step needs to be something other than simple ascending order. For example, repeat may be used to execute the body for every third element:

for ($i = 0; $i -lt $processes.Count; $i += 3) { 
    Write-Host $processes[$i].Name
}

The loop parameters may also be used to reverse the direction of the loop, for example:

for ($i = $processes.Count - 1; $i -ge 0; $i--) { 
    Write-Host $processes[$i].Name
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.108.11