为什么在 PowerShell 中 for 循环中省略变量会导致死循环?
Why does omitting variable in for cycle in PowerShell results in an endless loop?
为什么
$i=1
for ($i -le 5; $i++)
{Write-Host $i}
导致无限循环?
我从来没有尝试用 C# 或任何其他编程语言编写这样的东西,所以我不知道它在那里会如何表现,但为什么 for 循环不只是获取 "i" 变量将其与 5 进行比较,添加1再对比一下,for循环是盲目的还是某种形式的机器,而不是一个理性的、有逻辑的人。
为什么是无穷大而不只是抓取预定义的 i?
"because that's how PowerShell functions"之类的回答没用,我想知道为什么会这样
我知道它变成无限的,因为它缺少第一个参数,我想知道为什么,不过,喜欢哲学的答案"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?"
如果您阅读 Windows PowerShell 语言规范,您将看到 for 语句的语法:
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
这意味着在您的示例中,第一条语句是 INITIALIZER。
如果您这样重写循环:
$i=1
for (;$i -le 5; $i++)
{Write-Host $i}
它将如您所愿地工作。 注意附加的“;”。
如果省略“;”,在上面的文法中$i++
对应于for-condition,它不断地计算为$true,所以循环永远不会结束.
在你的编辑中,你说你知道它会变得无限,但你想知道为什么。
答案是 PowerShell 会自动进行类型转换。
您从值为 1 的 int
类型开始,该值递增。当它达到 [int]::MaxValue
并递增 1 时,类型 int
不能再保存此值。然后,PowerShell 自动将其类型转换为 double
。例如,试试这个:
$i=[int]::MaxValue -2
++$i
$i.GetType()
++$i
$i.GetType()
++$i
$i.GetType()
++$i
$i.GetType()
查看输出,PowerShell 将类型从 int
转换为 double
。
a double
的最大值加1等于自身,因此循环永远不会结束。
为什么
$i=1
for ($i -le 5; $i++)
{Write-Host $i}
导致无限循环? 我从来没有尝试用 C# 或任何其他编程语言编写这样的东西,所以我不知道它在那里会如何表现,但为什么 for 循环不只是获取 "i" 变量将其与 5 进行比较,添加1再对比一下,for循环是盲目的还是某种形式的机器,而不是一个理性的、有逻辑的人。
为什么是无穷大而不只是抓取预定义的 i? "because that's how PowerShell functions"之类的回答没用,我想知道为什么会这样
我知道它变成无限的,因为它缺少第一个参数,我想知道为什么,不过,喜欢哲学的答案"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?"
如果您阅读 Windows PowerShell 语言规范,您将看到 for 语句的语法:
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
这意味着在您的示例中,第一条语句是 INITIALIZER。
如果您这样重写循环:
$i=1
for (;$i -le 5; $i++)
{Write-Host $i}
它将如您所愿地工作。 注意附加的“;”。
如果省略“;”,在上面的文法中$i++
对应于for-condition,它不断地计算为$true,所以循环永远不会结束.
在你的编辑中,你说你知道它会变得无限,但你想知道为什么。 答案是 PowerShell 会自动进行类型转换。
您从值为 1 的 int
类型开始,该值递增。当它达到 [int]::MaxValue
并递增 1 时,类型 int
不能再保存此值。然后,PowerShell 自动将其类型转换为 double
。例如,试试这个:
$i=[int]::MaxValue -2
++$i
$i.GetType()
++$i
$i.GetType()
++$i
$i.GetType()
++$i
$i.GetType()
查看输出,PowerShell 将类型从 int
转换为 double
。
a double
的最大值加1等于自身,因此循环永远不会结束。