for loop

The for loop is a loop that normally iterates a value and executes the same script with the iterated numeric value. The basic syntax of a for loop is as follows:

# basic syntax of a for-loop
for
(<init>; <condition>; <repeat>)
{
<statement list>
}

It starts with the for keyword, followed by an init statement, a condition, and a repeat statement. To give you a better overview, let's take a look at a simple example:

# Simple example - for-loop
for ($i = 0; $i -lt 5; $i++)
{
    $i 
}
# 0, 1, 2, 3, 4

The init statement in our example is $i = 0, where we initialize an iterating variable, $i. The loop will run as often as the condition is fulfilled. In our example, with $i -lt 5, our loop will run 5 times, and the result will  be 0, 1, 2, 3, 4 because, for every run, $i is returned. The repeat statement will be executed after each run. As you have learned, $i++ is a unary operator, and will increase $i by 1. Alternatively, you could also write $i = $i +1 .

The next example shows you this approach:

# Simple example - for-loop - different iterator
for ($g = 0; $g -le 20; $g += 2)
{
Write-Host $i
}
# 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20
..................Content has been hidden....................

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