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

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,521 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 :)