- So far we have discussed multiple topics related
to variables, where to declare them,
when, how to name them.
Let's see how they're actually used
in expressions in simple assignment statements.
What is the simple assignment statement?
It's the statement that first and foremost
changes the value of a variable.
We have to do that following certain rules.
Most instances will be dictated by the mathematical
nature of an assignment statement,
which is the case with C because a lot
of programming in C is meant to provide solutions
in areas that are mathematics related.
For example, size = 12 we simply say
that the variable size will be assigned the value 12
or time = time + 1.
Notice that the right-hand side of this equation
and the left-hand side both use the variable time.
This is perfectly fine.
We simply say take the original value
of time on the right side, add one to it
and store it back as a new value for time
which is now incremented by one.
There is a simple way to accomplish the same thing
in C which is a shortcut notation,
simply states time +=1.
You may as well write time +=5,
time -= and so on.
Finally, a more complex formula, for example,
would be finalPrice = originalValue
+ tax - discount.
This is still a simple assignment statement
and we will gradually ease our way
into building more complex ones.
Let's see how this is done in code.
We are first declaring several
variables and initialize them.
Notice one other thing.
C is actually running on a very powerful laptop,
personal computer, Mac or Windows.
They are much more powerful than the pocket calculator.
In fact, if you have some experience with Math,
and improve your knowledge in programming
you can duplicate and even expand beyond
anything a simple calculator and even the most
advanced graphic calculators can do.
So this is a simple illustration of that.
Let's look at an example from the financial area.
We are declaring four variables,
original price, tax and discount.
The plan for this example is to show
how can you calculate the amount
of tax and discount and the final price to a customer.
First we're going to ask the customer
to enter the original price.
Then we'll ask the customer to enter the percentage
of tax, discount percentage and based
on that we will calculate the final price.
In this particular example, I chose to simply say,
"Let's first calculate the after tax price"
which is a simple mathematical
formula some of you may have known.
If not, here's a quick refresher.
You simply multiply the original price by one
plus the percentage of the tax you entered.
Then, to get the discount in this particular example,
I decided that the discount is
applied to the original price.
So I'm using the original price
as my base both for the tax and for the discount.
When all this is computed, on line 36
I am now able to calculate the special price,
which is simply the price after tax
minus the discount.
Finally, in line 38, I am printing out the rules
for the user to know how exactly I calculate that
not the formula, just the main idea.
Then I proceed with formatting.
Notice a few new things about the printf command.
Look at line 40.
First of all, I am printing the dollar sign
right before the number because, after all,
I am dealing with monetary values.
Second, notice after the percent there's a dot
and then 2 and then lf.
The lf you have seen before.
It simply denotes that original price is double
the dot and two it's another indicator to the printf
that I'm only interested in displaying
two decimal points, after all, we're dealing
with money and money have their
own characteristics when being displayed.
We expect to see a dollar sign and, typically,
we expect to see two decimal digits.
Let's run this program and see what it does.
(click)
First I'm being asked to enter a price.
I will enter 100 so it's easier for us to track.
The sale percentage, in my case, I decided
that the sale percentage would be eight.
The discount, let's say I'm going
for a modest discount of five.
The program indeed prints the rules.
The tax are applied first.
The discount is applied to the original price
and notice that I'm displaying the price after tax
as eight percent which is eight dollars out of a hundred
so we know the calculation is correct.
The discount amount is five dollars
because five percent out of a hundred is five.
Therefore, 108 minus five yields
the final special price of 103.
Let's run this one more time with different values.
I'm still going to put 100.
I'm going to add a ridiculous tax 400 percent
and an equally ridiculous discount.
The computer doesn't know that those number are ridiculous.
Only you know that so the computer will
simply take your numbers, put them
in the formula and give you the result.
The original price is 100.
The price after tax is 500 and so on.
Let's do one more test.
This time I will type something
that I know for sure is wrong.
I'm going to type a sales tax percentage
of 20 but a ridiculous discount
of 1,000 percent.
Let's see what happens.
Well, as expected, remember the term
earlier garbage in, garbage out?
Well, now my special price is negative.
If you enter a negative number,
this means that you are actually
expected to be paid money back.
Again the computer doesn't know any of that.
The computer simply takes the numbers you give it,
plugs them into the formulas that you coded,
performs the assignment statement
and displayed the result.
It is up to you to ensure that the input is valid,
the values make sense and they're obviously
mechanisms in C that will let you do just
that as we will see in our next videos.