"C# Basics (Retired)" was retired on June 30, 2019.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed C# Basics!
You have completed C# Basics!
If you have some code that you want to run when the "if" statement doesn't run, you can add an "else" clause.
- If the condition of an
ifstatement is false, the code in its body won't run.
bool ifCondition = false;
if (ifCondition)
{
Console.WriteLine("if clause");
}
- If you have some code that you want to run when the
ifstatement doesn't run, you can add anelseclause. - You add the
elsekeyword right after theifstatement's block, followed by another curly brace block with one or more lines of code that you want to run instead of theifstatement's code. - C# doesn't require it, but it's conventional to have the
elsekeyword aligned with theifkeyword, and theelsecode indented one level. Again, it just makes the code easier to read.
else
{
Console.WriteLine("else clause");
}
- If the
ifstatement's condition is true, then theelseclause won't run:bool ifCondition = true;[then change back] - If you have other conditions that you want to test only when the
ifcondition is false, you can add one or moreelse ifclauses between theifandelseclauses. - You add the keywords
elseandif, another condition expression, and some code you want to run if that condition is true. - As with the
elseclause, theelse ifkeywords should be lined up with theifkeyword, and the code following it should be indented one level.
bool ifCondition = false;
// new
bool elseIfCondition = true;
// old
if (ifCondition)
{
Console.WriteLine("if clause");
}
// new
else if (elseIfCondition)
{
Console.WriteLine("else if clause");
}
//old
else
{
Console.WriteLine("else clause");
}
- If the
ifclause istrue, then neither theelsifclauses nor theelseclause will be run. - If the
ifcondition and allelse ifconditions are false, then the else clause will be run. - The
elseclause isn't required. If all conditions are false and there's noelseclause, then no code will be run.
Let's use what we've learned to fix our store program's Price function.
static double Price(int quantity)
{
double pricePerUnit;
if (quantity >= 100)
{
pricePerUnit = 1.5;
}
else if (quantity >= 50) // Add "else"
{
pricePerUnit = 1.75;
}
else if (quantity < 50) // Add "else"
{
pricePerUnit = 2;
}
return quantity * pricePerUnit;
}
- This addresses the issue where quantities over 100 would be assigned a
pricePerUnitof $1.50 and then getpricePerUnitreassigned to $1.75.- The
else ifcondition only gets evaluated when theifcondition is not true. - So if quantity is 100 or more, the
ifcondition will be true. -
pricePerUnitwill get assigned $1.50. - And although a quantity of 100 is also greater than a quantity of 50, the
else ifcondition will never even get evaluated, because theifcondition was true.
- The
- So that's one problem fixed.
- But we're still getting the error "Use of unassigned local variable 'pricePerUnit'".
- That's because as far as C# can tell, it's still possible to reach the
returnstatement, wherepricePerUnitgets accessed, without having assigned to it. - We need to set up our code in such a way that
pricePerUnitwill get assigned to, no matter what. - You and I can actually tell that
pricePerUnitwill be assigned no matter what.- If the quantity isn't greater than or equal to 100...
- And it also isn't greater than or equal to 50...
- Then it for sure will be less than 50.
- So one of these three branches is always going to be run.
- But C# can't tell this. And so it's giving us an error.
- To fix this, let's try removing the
ifand the condition from this lastelse ifclause.
static double Price(int quantity)
{
double pricePerUnit;
if (quantity >= 100)
{
pricePerUnit = 1.5;
}
else if (quantity >= 50)
{
pricePerUnit = 1.75;
}
else
{
pricePerUnit = 2;
}
return quantity * pricePerUnit;
}
- We really don't need it, because if
quantityisn't greater than or equal to50, it must be less than50.- So now all we have is an
elseclause. Even if theifand theelse ifclauses don't run, theelseclause will. - Let's try running this now.
- The error goes away, and our program appears to be working correctly.
- Since
pricePerUnitgets assigned in all three of these scenarios, C# can tell thatpricePerUnitis going to have a value when it gets accessed, no matter what.
- So now all we have is an
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up