2

Why does

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

result in an infinite loop? I never tried to write something like this in C# or any other programming language, so I don't know how it will behave there, but why would the for cycle not just grab the "i" variable compare it to 5, add 1 to it and compare it again, it's like the for cycle is blind or some form of a machine, instead of a reasonable, logical human being.

Why infinite and not just grab predefined i? Answers like "because that's how PowerShell functions" are useless, I want to know why it works like that.

I know it becomes infinite, because it's missing the first parameter, I want to know why, though, like the answer the the philosophical "why would the for cycle not look for the variable of the same name outside of its cycle, but must be specifically included in it?"

0

2 Answers 2

4

If you read the Windows PowerShell language specifications, you'll see this grammar for the for statement:

for-statement:
for   new-linesopt   (
        new-linesopt   for-initializeropt   statement-terminator
        new-linesopt   for-conditionopt   statement-terminator
        new-linesopt   for-iteratoropt
        new-linesopt   )   statement-block
for   new-linesopt   (
        new-linesopt   for-initializeropt   statement-terminator
        new-linesopt   for-conditionopt
        new-linesopt   )   statement-block
for   new-linesopt   (
        new-linesopt   for-initializeropt
        new-linesopt   )   statement-block
for-initializer:
pipeline
for-condition:
pipeline
for-iterator:
pipeline

Meaning that in your example, the first statement is the INITIALIZER.

If you rewrite your loop this way:

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

it will work as you expect. Note the additional ";".

If you omit the ";", in the grammar above $i++ corresponds to the for-condition which constantly evaluates to $true, so the loop never ends.

Sign up to request clarification or add additional context in comments.

1 Comment

@mathgenius It seems you misunderstood the answer. It's not because it doesn't use variable outside the for-block, but it's because $i -le 5 becomes the initializer (though I don't know what happens), and $i++ becomes the checking condition (which always evaluate to true, according to the answer), leaving the third parameter empty (no iterator, though i is incremented in checking condition).
1

In your edit, you say you know it becomes infinite, but you want to know WHY. The answer, is that PowerShell does, automatically, type conversion.

You start with an int type with a value of 1, which is incremented. When it reaches [int]::MaxValue and is incremented by 1, the type int cannot hold this value anymore. Then, PowerShell converts its type, automatically, to double. For instance, try this:

$i=[int]::MaxValue -2

++$i
$i.GetType()

++$i
$i.GetType()

++$i
$i.GetType()

++$i
$i.GetType()

Look at the output, and see PowerShell converts the type from int to a double.

The maximum value of a double incremented by 1 is equals to itself, thus, the loop will never end.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.