- We just finished looking at
basic assignment statements
so now it's a right step forward
to look at arithmetic operators
and see how we can create more sophisticated
mathematical calculations in C.
C defines a series of operators as follows.
Addition, subtraction, multiplication
division, modelers and then two special ones.
Increment integer by one
and decrement integer by one.
Those operators work exactly as you would expect
coming from algebra or calculus.
The increment and decrement are not
commonly found in a typical mathematics class
so we'll see how useful they are in a C program.
This is an example of usage of multiple operators
and we'll see how they can be used
to perform some calculations, simple at first
and increasingly complex later.
We are defining two simple ints
five and seven in line 22 and 23
and a series of variables
sum, difference, product etc
that will store the results of those calculations.
We have already seen in a previous example
how to calculate percentages
and do a simple multiplication.
We'll just take this one step further
and point some things that are particular to C
and there are very important details to consider
when you create more complex programs.
We are simply going to take
each one of those operators
perform it separately
just so you can have an idea
what they do and how they work
and this is pretty simple.
Line 26, we simply add X+Y.
Line 30 we're adding X-Y
and then we are printing the results and so on.
Line 33, we are multiplying.
The star is the symbol used in C for a product.
Division and the modulus.
This simply gives you the remainder
if you divide X by Y.
Those are simple.
They work exactly how you would have expected
with basic knowledge of algebra.
It's more important to focus on line 42 and line 45.
It is very important to notice
the position of the ++ sign and the X.
If you place the ++ before the X
this means that you are incrementing
and then you are using the value.
45 means that you are decrementing
and then you are using the value
and we will note those differences shortly.
We will look at some code in line 50
that will illustrate how the increment works
and what is the meaning of
increment first, use after
or first use the variable and then increment it.
The best way to understand this
is simply run the program and then look at the code.
Let's scroll all the way up
and I will look at the code
in parallel with its output.
We have initially a sign in line 22
X to be equal five, Y to be equal seven
and indeed our output shows that.
Then we're displaying the sum, the difference
the product, the division.
Notice that the division is zero.
Why is five divide by seven zero?
The reason is we defined those as integer numbers.
When you are defining all variables as integers
their value is rounded up to the nearest integers.
Since five divide by seven is a fraction
that starts with zero point something
when this is truncated
then the division result is zero.
Everything else was as expected
so let's focus on the increments.
If you look at the increment in line 42
this is the result displayed here
at the increment value of X=6.
Originally X=5.
I incremented X, therefore its new value is six
as is displayed.
Then, I decremented X by one
and now its new value is back to five.
So far so good.
Let's take this one notch higher.
In line 51, which corresponds to this output here.
Line 51 I am assigning one to X
and notice that the ++ symbols are to the left of X
where am now printing the value of X in line 53.
The result is two.
In line 54 I'm printing again the value of X
but note that the ++ signs are to the right of X.
This tells the C compiler
"Use X and increment its value by one."
Therefore, when I print X at 54
I'm still printing its original value of two.
When I print X again in line 55
X is now three.
So line 54 did two things.
It printed the current value of X
and incremented it, but that increment
was not reflected in the print.
It's only in the subsequent use of X
that you saw the results of it.
We will now do the same thing
with the decrement operator.
Again, in line 59 I will assign one to X.
I will decrement it by one
and then print it and as expected, it's zero.
Then in line 62 I'm going to
decrement it again and print it.
However, the decrement is after the X
which means, use the value which is currently zero
print it out, decrement it.
In line 63 I'm now displaying the new value of X
which is decremented by one
and now X is negative one.
To recap this concept
all you have to remember is
if the symbols of addition or subtraction
are before the variable
they're executed first
and then the variable is assigned the new value
in whatever formula is being used.
If the symbols are written to the right of the variable
this means that the variable
is used in its current value
and then it is incremented or decremented
based on what the symbol is.