user1863886
user1863886

Reputation:

If statement in an if statement

For an assignment I have to prove that if statements can be placed within if statements in a Powershell script. Therefore I made the enclosed script. I am however not sure whether the script I have describes two separate if statements or would be considered an if within an 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

Any help on this one?


Thank you for your answer. Why does this not work however?

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” }

Upvotes: 0

Views: 7973

Answers (1)

pbhd
pbhd

Reputation: 4467

Nah, these are two separate if statements. Nested would be:

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

Upvotes: 2

Related Questions