Loops

In any language iterating operation is very important that's why most of the languages support looping structures. Same way PowerShell also supports looping structures to execute same code several times. PowerShell has two options for this "for" statement and "while" statement. We can say "for" loop and "while" loop. Let's go through few examples

Example
for ($number = 1; $number -le 5; $number = $number + 1)
{
    echo $number;
}

Above code will print the result as below

1
2
3
4
5

It has the same syntax as C programming language. For statement has 3 parts as below

Initialization: First part is variable initialization, we need to initialize the interator variable $number in this part, in the above example we have initialized it with 1 but we can assign any value. Please note that in PowerShell variable declaration is not needed, so whenever you start using variable it would be declared automatically. Condition: Second part is condition, we need to check for the condition, if condition evaluates true then it will execute the code inside for loop else it will complete the loop and execution flow will execute the next statement outside the for loop. Increment/Decrement: Third part is either increment or decrement, everytime it executes the "for" statement it will either increment or decrement the looping variable which will be used to check in the condition next time. We can increment or decrement variable by 1, 2 or any value. We can also shorten the increment part like $number = $number + 1 to $number++.

Let's look at how to print all the elements from the array of days using for statement

Example
$days = "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat";
for ($index = 0; $index -lt $days.length; $index++)
{
    echo $days[$index];
}
Above code will print the result as below

Sun
Mon
Tues
Wed
Thur
Fri
Sat

We have used length property of an array object, length property of array will return the number of elements in the array, In our array there are 7 elements so "for" statement will execute 7 times and it will print value of an element at specified 'index'.

The while statement

The while statement is another way of executing same code multiple times, it's a simple version of the "for" statement. "while" loop contains only 1 part and it's a condition, as long as condition evaluates true then "while" statement will be executed.

Example
$number = 1
while ($number -le 5)
{
    echo $number;
    $number = $number + 1;
}
Above code will print the result as below

1
2
3
4
5

Break statement

There are some circumstances when we need to check the condition and decide whether we want to continue iteration or break it, for this we can use break statement.

Example
$number = 1
while ($True)
{
    echo $number;
    $number = $number + 1;
    if ($number -gt 5)
    {
        break;
    }
}
Above code will print the result as below

1
2
3
4
5

Continue statement

In the loop if we want to skip some iteration based on the condition then continue statement will be used, when continue statement gets executed then it will skip the rest of the code in the loop and again start the execution from the begining of the loop. For example

Example
$number = 0
while ($number -lt 10)
{
    $number = $number + 1;
    if ($number % 2 -eq 0)
    {
        continue;
    }
    echo $number;
}
Above code will print number all odd number between 1 and 10.

 

← Previous

Next →