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

I can't seem to get past this one error in this C# challenge.

This error message keeps coming up and I have hit a wall.

StudentsCode.cs(17,2): error CS1525: Unexpected symbol `else' Compilation failed: 1 error(s), 0 warnings

I can't see the unexpected symbol it is breaking at.

Hi Marie,

Can you post your code so that we can take a look?

If I had to guess then I'd ask if you have used an 'else' where you needed an 'else if'.

1 Answer

Jeremy Faith
PLUS
Jeremy Faith
Courses Plus Student 56,696 Points

You should post some code. You may have nested the else-statement inside either the if-statement or the else if-statement.

For example:

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

// missing closing bracket
else 
// missing opening bracket

  Console.WriteLine("Too hot!");
}

The passing code should look similar to the following:

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

Hope this helps!

Steven Parker
Steven Parker
231,248 Points

Interesting code colors there, Jeremy. Try putting "C#" after the three back-ticks indicating the start of the code section.

Yes, I think that's just the default code markup for 3 backticks.

System.Console.Write("Hello World");

Is it c# or cs

System.Console.Write("Hello World");
System.Console.Write("Hello World");

Interesting, it looks like both work :-)

Thank you for your help! It got me successfully past my roadblock. I now realize that when I thought it had attached my code when I submitted my question it actually didn't. For the sake of documentation and closure, here is the error that came up when I had recreated it before applying any community help:

StudentsCode.cs(8,2): error CS1525: Unexpected symbol `else' Compilation failed: 1 error(s), 0 warnings

string input = Console.ReadLine();

  else
  {
    int temperature = int.Parse(input);

    if (temperature <= 21)
    {
      Console.WriteLine("Too cold!");
    }

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

Again, thank you for everyone's help and I will next time be sure to attach my code!

Marie