Share via


Loop-de-Loop

Here’s a quick trivia question for you:

Given the following two looping constructs:

 foreach ($item in $array)
{
    Do-Something -With $i;
}

and

 for ($i = 0; $i -lt $array.Count; $i++)
{
    Do-Something -With $i;
}

What's the difference?

 function Do-Something
{
    param (
        [int]$With = 0
    );

    if ($With% 2)
    {
        $global:array += $With;
    }

}

[int[]]$array = @(0..9);

for ($i = 0; $i -lt $array.Count; $i++)
{
    Do-Something -With $i;
    $array[$i];
}

Esoteric, no?