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

Neil Gordon
Neil Gordon
8,823 Points

Logic and Operand question

string input = Console.ReadLine();
int temperature = int.Parse(input);

if (temperature < 21) {
System.Console.WriteLine("Too cold!"); 
}
else if(temperature >=21 && <= 22){
System.Console.WriteLine("Just right."); 
}    

else if (temperature >22) 

{
System.Console.WriteLine("Too hot!"); 

}
else {}

For the second conditional statement I am not sure what operand to use to get a value between to integers. I tried the following operands '|' , '||', & , &&

I also am not sure how to close of this loop besides the last else statement any thoughts?

CodeChallenge.cs
string input = Console.ReadLine();
int temperature = int.Parse(input);

if (temperature < 21) {
System.Console.WriteLine("Too cold!"); 
}
else if(temperature >=21 && <= 22){
System.Console.WriteLine("Just right."); 
}    

else if (temperature >22) 

{
System.Console.WriteLine("Too hot!"); 

}
else {}
Neil Gordon
Neil Gordon
8,823 Points
string input = Console.ReadLine();
int temperature = int.Parse(input);

if (temperature < 21) {
System.Console.WriteLine("Too cold!"); 
}
else if(temperature >=21 && <= 22){
System.Console.WriteLine("Just right."); 
}    

else if (temperature >22) 

{
System.Console.WriteLine("Too hot!"); 

}
else {}

please use this post the previous post is not right

2 Answers

Steven Parker
Steven Parker
231,269 Points

While a good alternative solution was proposed, I notice no one has explained what went wrong with your second conditional statement. So here's the answer to that question. Your code contained this line:

else if(temperature >=21 && <= 22){

The problem was just that the second part of this test was incomplete. It should have been:

else if(temperature >=21 && temperature <= 22){

You code would work fine with that one correction.

Now as far as efficiency improvements, the final empty else can be eliminated completely. The third test can be replaced with a simple else since the other two cases have already been tested. But neither of these were causing any trouble.

Neil Gordon
Neil Gordon
8,823 Points

thank you for the insight

Hi Neil,

My answer was:

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

The first if checks for a temperature of less than 21. The else if is only reached if the temperature is 21 or over because of that first if. So here you only need to test for 22 or under. Then lastly for anything else you can just say to hot.

Neil Gordon
Neil Gordon
8,823 Points

Thank you bothxp , your code was definitely more concise. That did the trick.