Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
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
if
statement 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
if
statement doesn't run, you can add anelse
clause. - You add the
else
keyword right after theif
statement's block, followed by another curly brace block with one or more lines of code that you want to run instead of theif
statement's code. - C# doesn't require it, but it's conventional to have the
else
keyword aligned with theif
keyword, and theelse
code indented one level. Again, it just makes the code easier to read.
else
{
Console.WriteLine("else clause");
}
- If the
if
statement's condition is true, then theelse
clause won't run:bool ifCondition = true;
[then change back] - If you have other conditions that you want to test only when the
if
condition is false, you can add one or moreelse if
clauses between theif
andelse
clauses. - You add the keywords
else
andif
, another condition expression, and some code you want to run if that condition is true. - As with the
else
clause, theelse if
keywords should be lined up with theif
keyword, 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
if
clause istrue
, then neither theelsif
clauses nor theelse
clause will be run. - If the
if
condition and allelse if
conditions are false, then the else clause will be run. - The
else
clause isn't required. If all conditions are false and there's noelse
clause, 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
pricePerUnit
of $1.50 and then getpricePerUnit
reassigned to $1.75.- The
else if
condition only gets evaluated when theif
condition is not true. - So if quantity is 100 or more, the
if
condition will be true. -
pricePerUnit
will get assigned $1.50. - And although a quantity of 100 is also greater than a quantity of 50, the
else if
condition will never even get evaluated, because theif
condition 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
return
statement, wherepricePerUnit
gets accessed, without having assigned to it. - We need to set up our code in such a way that
pricePerUnit
will get assigned to, no matter what. - You and I can actually tell that
pricePerUnit
will 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
if
and the condition from this lastelse if
clause.
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
quantity
isn't greater than or equal to50
, it must be less than50
.- So now all we have is an
else
clause. Even if theif
and theelse if
clauses don't run, theelse
clause will. - Let's try running this now.
- The error goes away, and our program appears to be working correctly.
- Since
pricePerUnit
gets assigned in all three of these scenarios, C# can tell thatpricePerUnit
is 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
If the condition of an if statement is
false, the code in its body won't be run.
0:00
If you have some code that you want to
run when the if statement doesn't run,
0:08
you can add an else clause.
0:13
You add the else keyword right after the
if statement's block followed by another
0:14
curly brace block, with one or
0:19
more lines of code that you want to run
instead of the if statement's code.
0:20
C# doesn't require it, but
it's conventional to have the else keyword
0:24
aligned with the if keyword, and
the else code indented one level.
0:29
Again, it just makes
the code easier to read.
0:34
Let me Save this and try running it.
0:37
So because the if condition
variable is set to false,
0:42
the if clause does not run but
the else clause does run.
0:46
If the if statement's condition is
true then the else clause won't run.
0:54
So let me try running this.
1:00
And this time you'll see that the if
clause will run and the else clause won't.
1:03
Let me change that back.
1:08
If you have other conditions that you
want to test only when the if condition
1:12
is false, you can add one or more else if
clauses between the if and else clauses.
1:18
You add the keywords else and if,
another condition expression, and
1:24
some code you want to run
if that condition is true.
1:28
As with the else clause,
1:36
the else if keywords should be
lined up with the if keyword.
1:37
And the code following it
should be indented one level.
1:41
So let's try running this.
1:45
So the if condition variables set to
false so the if condition doesn't run.
1:49
But the else if condition
variable is set to true.
1:54
So this evaluates to true,
and this code does run,
1:58
printing the phrase else if clause.
2:02
And because the else if clause
runs the else clause does not run.
2:05
If the if clause is truem then
neither the else if clauses nor
2:17
the else clause will be run.
2:21
And if the if condition and
all else if conditions are false,
2:27
then the else clause will be run.
2:31
The else clause isn't required.
2:41
If all conditions are false, and there's
no else clause, then no code will be run.
2:44
Let's use what we've learned to fix
our store program's price function.
2:53
We'll add an else clause here, so
that this code will only be evaluated
2:58
if the quantity is not greater than or
equal to 100.
3:03
We'll also add an else clause here so
3:06
that this code will only be valuated if
the quantity is neither greater than or
3:09
equal to 100, or
greater than or equal to 50.
3:14
This addresses the issue where
quantities over 100 would be assigned
3:18
a pricePerUnit of $1.50, and
then get pricePerUnit reassigned to $1.75.
3:22
The else if condition only gets evaluated
when the if condition is not true.
3:28
So if quantity is 100 or more,
the if condition will be true.
3:33
Price per unit will get
assigned to a $1.50.
3:37
And although a quantity of 100 is also
greater than a quantity of 50, the else
3:41
if condition will never even get evaluated
because the if condition was true.
3:46
So that's one problem fixed.
3:50
But if we try to run this,
we'll see there's still a problem.
3:52
We're still getting the error use
of unassigned local variable price
3:58
per unit on line 28.
4:03
That's because as far as C sharp can tell,
it's still possible to reach the return
4:04
statement, or price per unit gets
accessed without having a sign to it.
4:09
We need to set up our code in such a way
that price per unit will get assigned to
4:12
no matter what.
4:17
You and I can actually tell that price
per unit will be assigned no matter what.
4:18
If the quantity isn't greater than or
equal to 100, and
4:22
it also isn't greater than or equal to 50,
the net for sure will be less than 50.
4:25
So one of these three branches
is always going to be run.
4:30
But C# can't tell this, and
so it's giving us an error.
4:34
To fix this, let's try removing the if and
4:37
the condition from this
last else if clause.
4:40
We really don't need it because
if quantity isn't greater than or
4:43
equal to 50, it must be less than 50.
4:47
So now all we have is an else clause.
4:49
Even if the if and else if clauses
don't run, the else clause will.
4:51
Let's try saving this and running it now.
4:56
The error goes away, and our program
appears to be working correctly.
5:00
Since price per unit gets assigned in all
three of these scenarios, C# can tell that
5:05
price per unit is going to have a value
when it gets accessed no matter what.
5:10
Let's try ordering different quantities to
see if the discount is applied correctly.
5:15
We'll try ordering 100 cans of cat food.
5:20
And in this case since
quantity is equal to 100,
5:23
the if clause will run and
set the price per unit to $1.50.
5:26
Let's try running it with 50 cans.
5:34
Since quantity is 15,
the else if clause runs, and
5:38
sets the price per unit to $1.75.
5:41
And lastly, let's try running
the program with ten cans.
5:46
Because quantity is not greater than or
equal to 100, and also not greater than or
5:53
equal to 50, the else clause runs,
and sets the price per unit to 2.
5:58
Our bulk discount feature
is working great.
6:02
Thanks to else if and else clauses,
6:05
we can be sure the price per unit
variable is set once and only once.
6:07
We've completed all of our features.
6:12
We display a welcome message to our user.
6:14
We prompt them for
the number of cans they want and
6:16
store what they enter in a variable.
6:19
Then we calculate a total price for
the order.
6:21
We also apply a discount if
they're ordering 50 or more cans.
6:24
Our program is done.
6:27
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