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

Could you check my work and tell me what is wrong with my code?

I do not understand whats the error message. It doesn't make any sens to me.

CodeChallenge.cs
string input = System.Console.ReadLine();
int temperature = int.Parse(input);
     if(temperature<21)
     {
         System.Console.WriteLine("Too cold!");
     }
    else if(temperature<=22)
    {
        System.Console.WriteLine("Just right.");
    }
    else(temperature>22)
    {
        System.Console.WriteLine("Too hot!");
    }


```Error :

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

What's the actual error message you are receiving?

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

2 Answers

andren
andren
28,558 Points

The problem is that else statements does not accept conditions. An else statement is automatically executed if the statements above it are not run. In this case you can simply remove the condition and your code will work fine since the previous statements cover temperatures equal to and below 22. Like this:

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

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

Thank you. It works now :)