I am going through Jerry Lee Ford’s book to study basic concepts of powershell and I have an exam coming up. I am however stuck at this for loop
for ($i = 1; $i -le 10; $i++) {
Write-Host $i
}
The way I see it:
$i equals 1 and as long as $i is less than or equal to then it gets upped by 1. So according to me, the end value would be 11 since the moment $i turns 10 it still matches the -le condition. However the end value is 10?
What am I overlooking here?
Let’s run through it:
$i = 1.
$i -le 10istrue. Print 1. Increment$ito 2$i = 2.
$i -le 10istrue. Print 2. Increment$ito 3$i = 3.
$i -le 10istrue. Print 3. Increment$ito 4…
$i = 9.
$i -le 10istrue. Print 9. Increment$ito 10$i = 10.
$i -le 10istrue. Print 10. Increment$ito 11$i = 11.
$i -le 10isfalse. We stop!!So you see from 1 to 10.
But if you saw the value of
$ioutside the loop, you will see that it is indeed 11. But the loop stopped printing at 10.