Skip to content

Loops

Loops allow a script to repeat an action multiple times. Each loop type has a specific purpose and syntax. This section explains how each loop works, how the syntax is structured, and provides examples that students can run directly on their machines.


1. for Loop

The for loop is used when you know exactly how many times you want to repeat something.

Its syntax mirrors C‑style loops:

for (<initialization>; <condition>; <iteration>) {
    <code>
}
  • initialization: runs once before the loop starts
  • condition: checked before each iteration
  • iteration: runs after each iteration

Example: counting from 1 to 5

for ($i = 1; $i -le 5; $i++) {
    "Iteration: $i"
}

Explanation:

  • $i = 1 sets the starting value.
  • $i -le 5 keeps the loop running while $i is 1 through 5.
  • $i++ increases $i by 1 each time.
  • The loop prints:
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5

Example: summing numbers

$sum = 0

for ($i = 1; $i -le 5; $i++) {
    $sum += $i
}

$sum

This calculates 1 + 2 + 3 + 4 + 5 and prints 15.


2. foreach Loop

foreach is used to iterate through each element in a collection (arrays, lists, command output).

Syntax:

foreach (<item> in <collection>) {
    <code>
}

Example: iterating through an array

$colors = "red", "green", "blue"

foreach ($color in $colors) {
    "Color: $color"
}

Explanation:

  • $colors contains three strings.
  • On each iteration, $color holds one element.
  • The loop prints each color.

Example: processing files

$files = Get-ChildItem

foreach ($file in $files) {
    $file.Name
}

Explanation:

  • Get-ChildItem returns an array of file objects.
  • $file.Name prints the name of each file.

3. while Loop

while repeats as long as a condition remains true.

Syntax:

while (<condition>) {
    <code>
}

The condition is checked before each iteration.

Example: count down from 5

$i = 5

while ($i -gt 0) {
    "Countdown: $i"
    $i--
}

Explanation:

  • $i starts at 5.
  • The loop runs while $i is greater than 0.
  • $i-- decreases the value each time.

Example: waiting for a condition

$number = 0

while ($number -lt 3) {
    "Number is $number"
    $number++
}

This prints:

Number is 0
Number is 1
Number is 2

4. do Loop

A do loop always runs at least once, because the condition is checked after the loop body.

There are two forms:

  • do { } while (<condition>)
  • do { } until (<condition>)

4.1 do … while

Runs the block, then repeats while the condition is true.

Example

$i = 1

do {
    "Value: $i"
    $i++
} while ($i -le 3)

Explanation:

  • The loop prints values 1, 2, and 3.
  • The condition is checked after each iteration.

4.2 do … until

Runs the block until the condition becomes true.

Example

$i = 1

do {
    "Value: $i"
    $i++
} until ($i -gt 3)

Explanation:

  • The loop runs until $i becomes greater than 3.
  • It prints the same output as the previous example.

5. Choosing the Right Loop

Loop Type Use When
for You know the exact number of iterations
foreach You want to process each item in a collection
while You want to repeat until a condition changes
do … while / until You need the loop to run at least once