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#

can someone tell what is wrong with my code in this challenge ? Thank you in advance.

Print "C# Rocks!" if language equals "C#" otherwise print language + " is not C#." So if I entered "Cheese" then "Cheese is not C#." would be printed to the screen.

Important: In each task of this code challenge, the code you write should be added to the code from the previous task.
CodeChallenge.cs

string language = Console.ReadLine(); ā€‹ if(language == "C#") { Console.WriteLine("C# Rocks!"); } else if(language == "Cheese"); { Console.WriteLine( "Cheese is not C#."); } else { Console.WriteLine("language + \"is not C#\""); }

ā€‹

1 Answer

Eric M
Eric M
11,517 Points

Hi Karol,

Congrats on making it to this coding challenge: https://teamtreehouse.com/library/c-basics/perform/if-else-3

Just as an FYI - You can format your code by using three backticks followed by the language name, then your code, then three backticks to close it of.

```cs

Your code here

```

So, here's what we've got right now:

string language = Console.ReadLine();
if (language == "C#")
{
    Console.WriteLine("C# Rocks!");
}
else if (language == "Cheese");
{
    Console.WriteLine("Cheese is not C#.");
} 
else
{
    Console.WriteLine("language + \"is not C#\"");
}

Let's focus on the syntax errors first.

In your last Console.WriteLine statement we want to print out whatever the user entered for language, followed by "is not C#". Our variables don't get enclosed in brackets, only our literals (string literals are strings that are literally typed into the program, rather than read from a file or entered by the user). We'll also add a fullstop at the end of our string to be exactly like the specification.

So we can change this line to be:

Console.WriteLine(language + " is not C#.");

On your else if line you have a semi-colon after the condition. Conditions lead into blocks that are enclosed in curly braces, the statements within the block must be terminated with semi-colons, but we don't terminate the conditional statement, we just open the block, so we need to remove this semicolon.

Okay, now we've got:

string language = Console.ReadLine();
if (language == "C#")
{
    Console.WriteLine("C# Rocks!");
}
else if (language == "Cheese")
{
    Console.WriteLine("Cheese is not C#.");
} 
else
{
    Console.WriteLine(language + " is not C#.");
}

The thing is though - even though this will pass the challenge - we've got some redudant code in here. The else if clause is not needed, because if someone types in "Cheese" our final else clause will print the same thing as our else if clause.

So, let's make this a bit neater by removing that branch:

string language = Console.ReadLine();
if (language == "C#")
{
    Console.WriteLine("C# Rocks!");
}
else
{
    Console.WriteLine(language + " is not C#.");
}

Now we've got something that passes the challenge and has a decent structure that makes sense.

Best of luck continuing through the C# track!

Cheers,

Eric