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

Getting a CS0019 error, don't know why.

Can anybody tell me why do I keep getting this error even though the temperature variable is set to int.

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

2 Answers

Tizmiz Tizzer
Tizmiz Tizzer
1,040 Points

Try changing else if (21 < temperature < 22) to else if (temperature == 21 || temperature == 22)

This should work since you are comparing the two values (21 and 22) and making sure one or the other is == to temperature, and if they aren't you will move down to the next if else/else statement.

Thank you! I kinda didn't understand it but then I twisted my mind and figured it out. I come from a python background so I hope you know why I did that lol.

Tizmiz Tizzer
Tizmiz Tizzer
1,040 Points

I started with python as my first language. I've made the same mistake before too, glad I could help!

Jon Wood
Jon Wood
9,884 Points

In this if statement: else if (21 < temperature < 22)

You need to compare each value to the temperature variable and use the and (&&) operator on it, like this: else if (21 < temperature && temperature < 22)

Thanks for your help. I tried to use the && operator but the challenge won't complete without some errors. So I tried the || operator and it passed. Thank you though for your help!

Jon Wood
Jon Wood
9,884 Points

Heh, my bad. I wasn't even thinking about the logic. I was just making sure I got that error fixed :p

Haha no problem man, thanks for the help :)

Jon Wood
Jon Wood
9,884 Points

And I just learned something from you! I had no idea you could do that in Python. I'm learning that, myself. :)