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

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,738 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!"); 
}