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) Perform if / else

Christopher Anderson
Christopher Anderson
1,601 Points

Print “C# Rocks!” if language equals “C#” otherwise print language + “ is not C#.” So if I entered "Cheese" then "Cheese

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.

CodeChallenge.cs
string language = Console.ReadLine();

if(language == "C#")
{
  System.Console.WriteLine("C# Rocks!");
}

else 
{
    System.Console.WriteLine("Bogus is not C#."); 
}
Christopher Anderson
Christopher Anderson
1,601 Points

WHY CANT I PUT
else { System.Console.WriteLine(language + " is not C#."); }

and why Bogus what does it mean

Hi Christopher,

"bogus" was what the teacher chose to test the else branch. Your code passes because that's what was being tested. I think you must have seen the error message and thought you were supposed to hard code bogus in the else part.

You wondered why you couldn't put System.Console.WriteLine(language + " is not C#.");

Did it not work when you tried that? That does pass and it's what the challenge is intending you to do. That way it responds properly to any language entered that's not "C#"

Tagging Jeremy McLain

Is it possible to get 2 tests on the else branch to prevent this?

Jeremy McLain
Jeremy McLain
Treehouse Guest Teacher

Yes "bogus" is just what the code challenge uses to test it. I can change it to test using a random word so that it doesn't confuse students in the future.

2 Answers

string language = Console.ReadLine();

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

Christopher, you would want to use string concatenation to grab the value of language if the response is not equal to C#. Bogus wasn't supposed to be used otherwise the program would assume the user typed Bogus.

Christopher Anderson
Christopher Anderson
1,601 Points

Thank you all for helping me, and thank you Mr.Mclain for being a great teacher and responding to my questions, i'm glad to be a Treehouse member.