Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
We've learned about comparison operators, so we can test whether the user wants to order 50 or more cans, or 100 or more. Now we just need to add code that applies a discounted price if those conditions are true.
We've learned about comparison operators, so we can test whether the user wants to order 50 or more cans, or 100 or more. Now we just need to add code that applies a discounted price if those conditions are true.
- Like most programming languages, C# has an
if
statement that executes some code only if a condition expression is true. - Starts with
if
keyword- followed by condition, which in these examples are the boolean values
true
andfalse
- followed by an opening curly brace
- followed by one or more lines of code (like a method body, these are usually indented to make them easier to read, although this isn't enforced)
- followed by closing curly brace
- followed by condition, which in these examples are the boolean values
- Because condition for first
if
alwaystrue
, code it contains will always be run. - Because condition for second
if
alwaysfalse
, code it contains will never be run.- We get an error when attempting to run it: "warning CS0162: Unreachable code detected"
- This isn't an actual error, so the program will run anyway, but C# does try to warn you that you've written code that won't be run under any circumstances.
if (true)
{
// This code will always be run.
Console.WriteLine("true");
Console.WriteLine("additional code here");
}
if (false)
{
// This code will never be run.
Console.WriteLine("false");
Console.WriteLine("additional code here");
}
- Just putting the values
true
andfalse
isn't very useful, though. - Actual programs will use expressions that evaluate to
true
orfalse
, like comparison operators.- Again, notice that we get a warning about the middle
if
statement. Because 75 is always going to be less than 100, the condition will always evaluate tofalse
, and the code will never be run.
- Again, notice that we get a warning about the middle
if (75 > 50)
{
// This code will always be run.
Console.WriteLine("75 > 50");
}
if (75 > 100)
{
// This code will never be run.
Console.WriteLine("75 > 100");
}
if (50 == 50)
{
// This code will always be run.
Console.WriteLine("50 == 50");
}
- Actual programs aren't likely to compare hard-coded numbers, either. Usually, they'll test the value of a variable.
- Notice that this time, the warning goes away.
- Because we're using a variable in our comparisons, we could theoretically change its value to something else:
int number = 50;
- If we do that,
if
conditions that previously evaluated tofalse
could change totrue
, and vice-versa, which is howif
statements usually work in practice. So there's nothing to warn us about.
- Because we're using a variable in our comparisons, we could theoretically change its value to something else:
int number = 75;
if (number > 50)
{
// This code will always be run.
Console.WriteLine("number > 50");
}
if (number > 100)
{
// This code will never be run.
Console.WriteLine("number > 100");
}
if (number == 50)
{
// This code will never be run.
Console.WriteLine("number == 50");
}
- Let's try using
if
in our program:
static double Price(int quantity)
{
if (quantity >= 100)
{
pricePerUnit = 1.5;
}
if (quantity >= 50)
{
pricePerUnit = 1.75;
}
if (quantity < 50)
{
pricePerUnit = 2;
}
return quantity * pricePerUnit;
}
- Whoops! We get errors:
Program.cs(10,13): error CS0103: The name 'pricePerUnit' does not exist in the current context [/home/treehouse/workspace/workspace.csproj]
Program.cs(14,13): error CS0103: The name 'pricePerUnit' does not exist in the current context [/home/treehouse/workspace/workspace.csproj]
Program.cs(18,13): error CS0103: The name 'pricePerUnit' does not exist in the current context [/home/treehouse/workspace/workspace.csproj]
Program.cs(20,27): error CS0103: The name 'pricePerUnit' does not exist in the current context [/home/treehouse/workspace/workspace.csproj]
- That's right, we forgot to declare the
pricePerUnit
variable before assigning to it. - I'll go up to the first instance and make it a declaration:
static double Price(int quantity)
{
if (quantity >= 100)
{
double pricePerUnit = 1.5;
}
if (quantity >= 50)
{
pricePerUnit = 1.75;
}
if (quantity < 50)
{
pricePerUnit = 2;
}
return quantity * pricePerUnit;
}
- Hmm, that's strange. Now we get three errors and a warning:
Program.cs(14,13): error CS0103: The name 'pricePerUnit' does not exist in the current context [/home/treehouse/workspace/workspace.csproj]
Program.cs(18,13): error CS0103: The name 'pricePerUnit' does not exist in the current context [/home/treehouse/workspace/workspace.csproj]
Program.cs(20,27): error CS0103: The name 'pricePerUnit' does not exist in the current context [/home/treehouse/workspace/workspace.csproj]
Program.cs(10,20): warning CS0219: The variable 'pricePerUnit' is assigned but its value is never used [/home/treehouse/workspace/workspace.csproj]
- This last warning especially is interesting: "The variable 'pricePerUnit' is assigned but its value is never used"
- We use the
pricePerUnit
variable right here when we're calculating a return value! Why is it saying it's never used?
- We use the
- Remember we showed you that variables declared within a function are only in scope within that function?
- The same is true for
if
statements. If you declare a variable within anif
statement's block, it's only in scope within that block.
- The same is true for
- The solution is to declare the variable before any of the
if
blocks. Then it will be in scope for all of them, as well as afterward.
static double Price(int quantity)
{
double pricePerUnit;
if (quantity >= 100)
{
pricePerUnit = 1.5;
}
if (quantity >= 50)
{
pricePerUnit = 1.75;
}
if (quantity < 50)
{
pricePerUnit = 2;
}
return quantity * pricePerUnit;
}
- But we still get an error when we use
pricePerUnit
to calculate a return value. It says "Use of unassigned local variable 'pricePerUnit'".- Why is that? We assign values to
pricePerUnit
in the lines above! - The problem is that we assign to
pricePerUnit
in theif
blocks. What if none of theif
blocks had a true condition? - None of them would run, and we'd get to the
return
statement with no value assigned topricePerUnit
! - Now, you and I know that's not actually possible. No matter what value is assigned to
quantity
, one of those threeif
statements is going to be true. A value will be assigned topricePerUnit
no matter what. - But C# doesn't know that. All it sees is a possibility that we'll reach the
return
statement with no value assigned topricePerUnit
. And the result is an error. - So we need a way to say that one of those three
if
statements will be evaluated, no matter what.
- Why is that? We assign values to
- There's an additional problem here, too.
- This code works fine for
quantity >= 50
andquantity < 50
, but ifquantity >= 100
,price_per_unit
gets set to1.75
because100
is also>= 50
.
- This code works fine for
Up next, we'll look at else if
and else
clauses for if
statements, which will help us fix these issues.
-
0:00
We've learned about comparison operators so
-
0:02
we can test whether the user wants to 50 or more cans, or 100 or more.
-
0:07
Now, we just need to add code that applies the discounted
-
0:09
price if those conditions are true.
-
0:12
Like most programming languages, C# has an if statement that executes some
-
0:17
code only if a condition expression is true.
-
0:20
It starts with the IF keyword, that's followed by a condition in parentheses,
-
0:24
which in these examples are the bullied and values true and false.
-
0:29
Then there's an opening curly brace, followed by one or more lines of code.
-
0:33
Like a method body, these are usually indented to make them easier to read,
-
0:37
although this isn't enforced.
-
0:39
And then finally, a closing curly brace to mark the end of the statements that should
-
0:44
be executed only if the condition is true.
-
0:46
Because the condition for
-
0:48
this first if statement is always true, the codec contains will always be run.
-
0:53
Because the code for
-
0:54
the second if statement is always false, the code it contains will never be run.
-
0:59
Notice we get an error when attempting to run this.
-
1:02
It's for line 14 and it says, unreachable code detected.
-
1:06
This isn't an actual error, so the program will run anyway, but C# does try to warn
-
1:11
you that you've written code that won't be run under any circumstances.
-
1:18
You can see the results in the output, if true, which is always going to be true, so
-
1:22
this code will always be run.
-
1:24
And it prints the strings true and additional code here.
-
1:30
Just putting the values true and false isn't very useful though.
-
1:34
Actual programs will use expressions that evaluate to true or
-
1:37
false, like comparison operators.
-
1:40
So because 75 is greater than 50, the result of this is true, and
-
1:45
this line of code right here will be run.
-
1:47
You can see 75 is greater than 50 here in the output.
-
1:51
Because 75 is less than 100, the result of this expression is false,
-
1:55
75 is not greater than 100.
-
1:57
So this line of code is not run and you can't see it in the output.
-
2:02
However, the result of testing whether 50 is equal to 50 is true, so
-
2:07
this line of code here will run, and you can see it here in the output.
-
2:13
Actual programs aren't likely to compare hard coded numbers either.
-
2:17
Usually, they'll test the value of a variable.
-
2:21
So here, we assigned the value 75 to a variable called number,
-
2:26
we then compare that variable to 50.
-
2:29
We then test whether number is greater than 50, and remember,
-
2:33
number contains 75, so this is going to be true.
-
2:36
And you can see that this code runs and
-
2:38
prints number greater than 50 in the output.
-
2:42
However, number is not greater than 100 and it's also not equal to 50, so
-
2:46
neither of this later two lines are going to run.
-
2:50
Let's try updating our cat food store program to use if statements.
-
2:55
We'll get rid of the return statement that simply returns the quantity multiplied
-
2:59
by two.
-
3:01
Instead, we'll test whether the quantity is greater than or equal to 100.
-
3:05
If it is, we'll set a variable called price per unit, 1.5, or $1.50.
-
3:10
We'll test whether the quantity is greater than or equal to 50.
-
3:14
If it is, we'll set price per unit to $1.75.
-
3:17
And finally, we'll test whether the quantity is less than 50.
-
3:21
If it is, we'll set the price per unit to two, finally,
-
3:26
we'll return the results of multiplying the quantity by the price per unit.
-
3:32
But if we save this, and
-
3:33
try running it, We get multiple
-
3:40
errors saying the name price per unit does not exist in the current context.
-
3:45
And we did forget to declare the price per unit variable before assigning to it.
-
3:50
So I'll go up to the first instance and make it a declaration.
-
4:03
But this still isn't working, now we get three errors and a warning.
-
4:14
This last warning is especially interesting.
-
4:17
It says, the variable pricePerUnit is assigned but its value is never used.
-
4:22
But we use the pricePerUnit variable right here when we're calculating
-
4:26
a return value, so why is it saying it's never used?
-
4:30
Remember we showed you that the variables declared within a function
-
4:34
are only in scope within that function?
-
4:37
The same is true for if statements, if you declare a variable within an if
-
4:41
statement's block, it's only in scope within that block.
-
4:45
So here, we declare a price per unit variable and assign a value to it.
-
4:50
But down here,
-
4:51
this refers to an entirely separate price per unit variable that doesn't exist.
-
4:56
Because we haven't declared a price per unit variable within this if block,
-
5:01
the same is true here.
-
5:02
This price per unit variable is an attempt to assign to a very bolt that doesn't
-
5:07
exist, because there's no price per unit declaration within this if block.
-
5:12
And finally, the price per unit variable that we attempt to use in this return
-
5:16
statement also doesn't exist within this block.
-
5:22
The solution is to declare the variable before any of the if blocks.
-
5:27
Then it will be in scope for all of them, as well as afterward.
-
5:32
So we add a declaration up here, and
-
5:34
then we can change this to an assignment rather than a declaration.
-
5:39
Let's try saving that, and running it.
-
5:43
But we still get an error here on line 28 saying,
-
5:46
use of unassigned local variable price per unit, now why is that?
-
5:51
We assign values to price per unit in the lines above.
-
5:57
The problem is that we assigned a price per unit within the if blocks.
-
6:02
What if none of the if blocks have a true condition?
-
6:05
None of them would run, and
-
6:06
we get to the return statement with no value assign to price per unit.
-
6:12
Now, you and I know that's not actually possible.
-
6:15
No matter what value is assigned a quantity,
-
6:17
one of those three if statements is going to be true.
-
6:21
A value will be assigned a price perPerUnit no matter what, but
-
6:24
C# doesn't know that.
-
6:25
All it sees is a possibility that will reach the return statement with
-
6:29
no value assigned to price per unit, and the result is an error.
-
6:34
So we need a way to say that,
-
6:35
one of those three if statements will be evaluated, no matter what.
-
6:39
There's an additional problem here, too.
-
6:42
If the quantity is greater than or equal to 50,
-
6:45
the pricePerUnit will get set to $1.75.
-
6:47
If the quantity is less than 50, the pricePerUnit will get set to $2.
-
6:51
This is as it should be, but if the quantity is greater than or
-
6:56
equal to 100, the pricePerUnit will first get set to 1.5.
-
7:00
But then this if statement will also run because a quantity greater than or
-
7:05
equal to 100 is also greater than or equal to 50.
-
7:08
So the price per unit will get set to $1.50 and then it'll be reset to $1.75.
-
7:15
So we also need a way to ensure that only one of these three if statements will run.
-
7:21
Up next we'll look at if statements, else if and else clauses,
-
7:25
which will help us fix these issues.
You need to sign up for Treehouse in order to download course files.
Sign up