if语句中的if语句

时间:2012-12-24 20:56:58

标签: powershell

对于作业,我必须证明if语句可以放在Powershell脚本中的if语句中。因此我制作了附上的剧本。但是我不确定我的脚本是否描述了两个单独的if语句,或者在if中是否被视为if。

[int]$a = Read-host "Please input a number." 
if ($a –eq 5) { 
Write-Host “a is 5” } 
if ($a –ne 5) { 
Write-Host “a isn't 5” }
Read-host

对此有何帮助?


感谢您的回答。为什么这不起作用?

if ($a –lt 2) { 
    if ($a –lt 3) {       
        if ($a –lt 4) { 
             if ($a –lt 5) {  
             Write-Host “a is less than 5” }
        Write-Host “a is less than 4” } 
    Write-Host “a is less than 3” }
Write-Host “a is less than 2” }

1 个答案:

答案 0 :(得分:2)

不,这是两个单独的if语句。嵌套将是:

if ($a –lt 5) { 
  if ($a –ne 2) { 
  Write-Host “a isn't 2” }
Write-Host “a is lt 5” }