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

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
231,248 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!