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 trialMichael Sawyer
2,086 PointsI'm struggling with the if/else objective question. If I could get a step by step to help me better understand please?
I've watched the if/else video a few times now. I understood and answered the "if" objective question without issues so I'm not sure why this one has me stumped. If someone could show me a step by step on how to answer the question correctly so I can write it down and learn from it, I would appreciate it, thanks! Until then, I'll keep trying.
Michael Sawyer
2,086 PointsSorry I didn't add this in the above question, this is new to me. Below is the question and C# code I'm working with:
Question: 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.
string language = Console.ReadLine();
if(language == "C#")
{
System.Console.WriteLine("C# Rocks!");
}
2 Answers
tjgrist
6,553 PointsHi Michael,
Your if block looks good. Now Let's look at how to construct the second part of the question/instructions: the "else" block.
The "else" block is a default -- it is what the computer will run as long as the "if" block's condition is false. (Remember - the condition is what is in () after your "if".
So, if the language that is entered by the user is not C#, the "if" block you have written will evaluate to false. What happens when a condition inside of an if block is false? Then the computer does not run the code inside it, and it skips to the next block, in our case it's going to be the "else" block.
Constructing an else block is a little different. Because it is the default, it does not need a 'condition' in parentheses, so you just write it like this:
else {
System.Console.WriteLine(language + " is not C#.");
}
Altogether it looks like this:
string language = Console.ReadLine();
if(language == "C#")
{
System.Console.WriteLine("C# Rocks!");
}
else
{
System.Console.WriteLine(language + " is not C#.");
}
//So here if the user typed in "Cheese", the computer would check to see if cheese is equal to C#. Since it is not, it would go to the default which is the else block and print "Cheese is not C#."
Hope that helps!
Michael Sawyer
2,086 PointsWow, did I overthink that! That makes sense, thanks for your help TJ!
tjgrist
6,553 PointsNo problem.
Steven Parker
231,236 PointsSteven Parker
231,236 PointsIt's hard to know what the problem you may be having is unless you post your code, along with a link to the challenge.