2

I have written the following PowerShell script below:

if ($Animal -eq "Chicken") {
    if ($Food -eq "Egg") {
        Write-Host "This chicken eats egg"
    } ElseIf ($Food -eq "Soup") {
        Write-Host "This chicken eats soup"
} ElseIf ($Animal -eq "Cow") {
    if ($Food -eq "Egg") {
        Write-Host "This cow eats egg"
    } ElseIf ($Food -eq "Soup") {
        Write-Host "This cow eats soup"
} ElseIf ($Animal -eq "Zebra") {
    if ($Food -eq "Egg") {
        Write-Host "This zebra eats egg"
    } ElseIf ($Food -eq "Soup") {
        Write-Host "This zebra eats soup"

The script works for the Chicken and the Cow but does not register the zebra portion of the code. I am not sure of what I am doing wrong as there are no errors being returned. Can someone please provide some guidance?

3
  • 2
    I'm not 100% sure what you are trying to achieve here. You would probably have more fun using a switch. Also, you don't need to check the food, just output the current value of the variable :) Commented Aug 30, 2019 at 15:00
  • 3
    The code sample you've posted is incomplete (and it most certainly won't work with $Animal = "Cow", because you've nested the elseif inside the first if statement). Please post code that can actually be executed :) Commented Aug 30, 2019 at 15:01
  • 5
    You should have the same number of }s as {s. Commented Aug 30, 2019 at 15:01

3 Answers 3

3

Whilst I fully agree with Doug's answer, I am completely lost as to why you are using if statements in the first place. If this is a learning exercise, I would recommend using a switch statement such as this:

$Animal = "Zebra"
$Food = "Soup"
$output = ""

switch ($Animal){
    "Chicken" { $output = "This $($Animal) eat $($Food)"; break}
    "Cow" { $output = "This $($Animal) eat $($Food)"; break }
    "Zebra" { $output = "This $($Animal) eat $($Food)"; break }
}

Write-Output $output

or just outputting the values without the unnecessary checking:

$Animal = "Zebra"
$Food = "Soup"

Write-Output "This $($Animal) eat $($Food)"
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, yes it is a learning exercise. I'm a beginner to PowerShell. Thank you for your feedback and recommendation.
2

You are missing the } after your ElseIfs.

Corrected code:

if ($Animal -eq "Chicken") {
    if ($Food -eq "Egg") {
        Write-Host "This chicken eats egg"
    } ElseIf ($Food -eq "Soup") {
        Write-Host "This chicken eats soup"
    }
} ElseIf ($Animal -eq "Cow") {
    if ($Food -eq "Egg") {
        Write-Host "This cow eats egg"
    } ElseIf ($Food -eq "Soup") {
        Write-Host "This cow eats soup"
    }
} ElseIf ($Animal -eq "Zebra") {
    if ($Food -eq "Egg") {
        Write-Host "This zebra eats egg"
    } ElseIf ($Food -eq "Soup") {
        Write-Host "This zebra eats soup"
    }
}

Better Way

That said, as @MathiasR.Jessen pointed out, this can be simplified using variable expansion:

Write-Host "This $($Animal.ToLower()) eats $($Food.ToLower())"

See About Quoting Rules for more info.

2 Comments

Could also be fixed with this one-liner: Write-Host "This $($Animal.ToLower()) eats $($Food.ToLower())" :-D
I am not the batman of question answering (I answer the question you want, not the question you need). Updated answer with your info ;)
1

Doug is correct, you missed some }

I have re-formatted it for you. I find this formatting helps to catch mistakes like this...

if ($Animal -eq "Chicken") 
{
    if ($Food -eq "Egg") 
    {
        Write-Host "This chicken eats egg"
    } 
    ElseIf ($Food -eq "Soup") 
    {
        Write-Host "This chicken eats soup"
    }
}
ElseIf ($Animal -eq "Cow") 
{
    if ($Food -eq "Egg")
    {
        Write-Host "This cow eats egg"
    }
    ElseIf ($Food -eq "Soup")
    {    
        Write-Host "This cow eats soup"
    }
} 
ElseIf ($Animal -eq "Zebra") 
{
    if ($Food -eq "Egg")
    {
        Write-Host "This zebra eats egg"
    } 
    ElseIf ($Food -eq "Soup") 
    {
        Write-Host "This zebra eats soup"
    }
}

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.