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

Riley Sutton
Riley Sutton
889 Points

C# Compiler error

New to programming. I cant seam to find out why im getting the following an error here.

StudentsCode.cs(18,2): error CS1525: Unexpected symbol `{' StudentsCode.cs(18,3): warning CS0642: Possible mistaken empty statement Compilation failed: 1 error(s), 1 warnings

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

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

2 Answers

Afrid Mondal
Afrid Mondal
6,255 Points

Hey Riley, you are doing great. But, remember never put condition on else block, because it's statement going to execute if any other statement is not matched the condition. I correct your code and it will run...

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

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

Think of the else block to be the default code block that runs when none of the above conditions are met (AKA, none of the conditional statements return true!) Afrid was spot on with this, your error is due to the conditional statement you wrote after the else keyword.

Steven Parker
Steven Parker
221,292 Points

:point_right: Just convert the unneeded test after the else into a comment:

  if (temperature < 21)
    Console.WriteLine("Too Cold!");
  else if(temperature <= 22)
    Console.WriteLine("Just right.");
  else //(temperature > 22)
    Console.WriteLine("Too hot!");

It fixes it, and it's a good programming practice. (Also for brevity I removed the braces - they aren't needed when there's only one statement anyway).

Riley Sutton
Riley Sutton
889 Points

Thank you Steven!