Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Basics (Retired) Perfect if / else if / else

ANDY VAN DEN BOS
ANDY VAN DEN BOS
2,330 Points

C# Basics, if/ if else/ else; //first task. What am I missing?

Two things,

I don't really understand how to read the Compiler Error Message.

So, as a result of that I am not sure what I am doing wrong.

Can C# brains help me out? It was all going so well up until now. DERP

CodeChallenge.cs
string input = Console.ReadLine();
int temperature = int.Parse(input);
if(temperature > 21)
{
Console.WriteLine("Too cold!");
}
else if(temperature = 21 != 22)
{
Console.WriteLine("Just right.");
}
else(temperature > 22)
{
Console.WriteLine("Too hot!");
}

2 Answers

Steven Parker
Steven Parker
231,248 Points

An else statement does not take an expression.

It automatically handles all cases not covered by the previous if and else if statements. But for clarity, you might want to show the case it handles as a comment:

else // (temperature > 22)

You also appear to have a syntax error on this line:

else if(temperature = 21 != 22)

When combining comparison expressions, both expressions must be complete (have left and right sides), and they would be combined with a logical operator such as AND ("&&") or OR ("||").

You might also take a closer look at your other comparisons and make sure the left and right sides are not reversed and/or that you are using the correct comparison operator.

ANDY VAN DEN BOS
ANDY VAN DEN BOS
2,330 Points

OOOOH okay, Dude, that makes way more seance now. I think I was over thinking this one trying to over complicate things.

Thanks again.

Kevin Oneill
Kevin Oneill
6,509 Points
string input = Console.ReadLine();
int temperature = int.Parse(input);

if (temperature < 21)
{
    Console.WriteLine("Too cold!");
}

else if (temperature > 22)
{
    Console.WriteLine("Too hot!");
}

else 
{
    Console.WriteLine("Just right.");
}
ANDY VAN DEN BOS
ANDY VAN DEN BOS
2,330 Points

Thank you Kevin,

I was trying to nuke it with operators and such. lesson of the day, K.I.S.S. //Keep it simple stupid.