Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Not sure what I'm doing wrong

I cant tell whats wrong with my code here it says unexpected '{'.

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

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

5 Answers

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,716 Points

I don't think the "unexpected {" is the real problem.

The "else" case is a default for all other cases, so you don't put a condition with that. (Alternatively, you could do another else if with another condition)

Gursimran,

Remove the else in the middle so it goes if, if, else. Add a period to the Just right. Finally, you should modify your conditional statement for "Just right." to an && to get the range (21 >= && <= 22)

if (temperature >= 21 && temperature <= 22)
{
  Console.WriteLine("Just right.");
}

Basically just replace your "else if" statement with that.

Hi Gursimran,

You were very close

I would suggest that for your 'else if' you only need to test for a value of 22 or less. Also the last 'else' will only be reached if the temperature is greater than 22 so you don't need the actual test.

These challenges can be very picky. So make sure that you have got the period after "Just Right"

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

Thanks all for your help, I see where i went wrong I've got it now.

Alan Mattanó
PLUS
Alan Mattanó
Courses Plus Student 12,188 Points

I'm a student but i think in an if statement you can not write a condition in the else

must be

else 
{
    // Code
}

with out a condition (true)

if you need it then use else if

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