Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
This video will teach you how to compare numbers to see if one is greater than the other, or if they're equal.
Cat Food Store Features
Display welcome messageAsk for quantityCalculate total- Discount for large orders
Our program always charges users 2 dollars per can. It takes the number of cans the user wants to order, and multiplies it by 2 to get the total cost.
Our last requirement is to provide a discount for large orders of cans. Specifically, if the order is for 100 cans or more, the price should be $1.50 per can. If it's for 50 cans or more, the price should be $1.75 per can. And all other orders should be the full price, $2 per can.
Our first problem here is to figure out which discount we should apply. Are they ordering 100 cans or more? 50 to 99? Less than 50? To do this, we're going to need a way to compare numbers.
- This code will be a little too complicated to embed within a string. So first, let's move the part that calculates a price out to a method.
- We'll add a
Price
function that takes the number of cans being ordered in a parameter namedquantity
. - Notice that we're having our function return a
double
floating-point value, not anint
. - An integer return value would work right now, because we're only returning whole dollar amounts.
- But when we add discounts later, we'll be using fractional dollar amounts like $1.75 or $1.50 per can.
- So we'll prepare for this by setting
Price
up with adouble
floating-point return type now. -
quantity * 2
is actually anint
value, butint
is a less-precise type anddouble
is a more-precise type, so the conversion fromint
todouble
will be performed implicitly.
- We'll add a
static double Price(int quantity)
{
return quantity * 2;
}
- Now that we've set the
Price
method up, we can make a call to it in theMain
method.- We pass it the number of cans, and store its return value in a new
total
variable. - Then we update our call to
Console.WriteLine
to use the newtotal
variable instead of embedding the calculation in the interpolated string.
- We pass it the number of cans, and store its return value in a new
double total = Price(number);
Console.WriteLine($"For {number} cans, your total is: ${total}");
Now that we've moved our price calculation out to a separate method, we can work towards applying a discount for large orders.
In order to know which discount to apply, we need to be able to tell if the order quantity is over 100, if it's between 50 and 100, or if it's below 50.
C# uses comparison operators to tell whether one value is equal to, greater than, or less than another.
==
!=
<
>
<=
>=
Comparison operators return a boolean value. Boolean values are either true or false. They're represented in C# code by the words true
and false
. Note that these are not strings, so they're not surrounded by quotes.
true
false
int quantity = 75;
Console.WriteLine(quantity > 50); // => true
Console.WriteLine(quantity < 50); // => false
Console.WriteLine(quantity == 75); // => true
- Don't confuse
==
with=
!
Console.WriteLine(quantity == 99); // => false
Console.WriteLine(quantity); // => 75
Console.WriteLine(quantity = 99); // Reassignment!
Console.WriteLine(quantity); // => 99
-
>=
,<=
Console.WriteLine(quantity >= 50); // true
Console.WriteLine(quantity <= 50); // false
quantity = 50;
Console.WriteLine(quantity >= 50); // true
Console.WriteLine(quantity <= 50); // true
-
!=
- The exclamation point can be read aloud as "not".
- So
!=
can be read aloud as "not equal". - It's the opposite of
==
.
Console.WriteLine(quantity != 50); // => false
Console.WriteLine(quantity == 50); // => true
- Don't try to compare a string to a numeric type! You'll get an error.
Console.WriteLine("3" == 3);
Console.WriteLine("4" > 3);
Operator '==' cannot be applied to operands of type 'string' and 'int'
Operator '>' cannot be applied to operands of type 'string' and 'int'
- You should only do a comparison if you can convert one class to match the other.
Console.WriteLine(int.Parse("3") == 3); // => true
Console.WriteLine(int.Parse("4") > 3); // => true
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