- At this point, we're going to continue our discussions
of operators, expressions and statements in C
and we'll see increasingly more complex
and more useful ways to calculate things.
Let us recall what are Complex Statements.
They are simply those that involve
the use of multiple expressions.
That's the only thing that makes them complex.
Sometimes, they represent a very complicated process
of thinking that's being reflected
in the complexity of a formula or a statement.
Let's look at an example.
We are defining four variables:
initValue, weight, k, and a flag,
which will then be used in the complex
statement of the bottom of the screen.
initValue, it's very simple.
An initial value of a variable which we decided to be zero.
Weight of five and k of one
are meant to add to that variable.
A repeated number of weight based on the value of k.
For example, if k equals one, we are just adding five.
If k was to equal two, we will be adding two times five.
If you look at your expression at the bottom,
you will see that weight is being multiplied by k.
There are additional variables that are multiplying
weight to the right and to the left.
Let's continue our discussion on the left side.
We are multiplying weight by k and incremented k by one.
So should we repeat that operation,
k will now be two and so on.
A flag does something very interesting in this case
and it's an example of combining
both arithmetic and logical expressions.
If you look at the declaration of flag, it receives
the value after evaluating whether k is less than 10.
If k is less than 10, the value
of this expression will be true.
Thus flag would equal one.
If k is more than 10, the value
of this expression will be zero
and thus flag will be zero.
Let's look again at the complex statement.
Flag multiplies weight and k.
It can have only values of one or zero.
Therefore, if k is equal or larger
than 10, flag will be zero.
Thus we are preventing from adding
to the initial value more than 10 times the weight.
This is an interesting way of putting a top boundary
on a value by simply defining a flag.
Problems like this are easily solvable
if you understand the meaning of truth tables, the meaning
of logical inferences, and how to combine the two.
Let's see how this is done in code.
In our code, we are going to do several things
to exemplify how this operation works.
At first, we are going to define several variables.
a, b and c with respective value
of five, negative three, and 12.
Then free floating point variables: r, s, and t.
Let's start with simple calculations
to illustrate complex expressions.
We will call this PART 1.
In PART 1, we are multiplying a times r.
It's a simple expression as a refresher.
You can simply store the result and a float.
Now we are doing something
that we saw in our previous screen.
We are multiplying (a > b)*c.
In other words, we are calculating
the value of (a > b).
In this case, if you scroll up, you will notice
that five is greater than negative three.
Therefore, the C compiler evaluates
(a > b) as one.
Therefore the expression on line 35
becomes one times c
and then the value of c is being displayed.
In the same manner, we can do a different
calculation by using (a < b) which in this case
would be zero since we define a to have a lower value.
In PART 2, we are going to, again, play with the notion
of incrementing the variable and then using it.
In PART 3, we will be using the variable
and then increment it.
Let's follow the execution of this code
in parallel to looking at it.
It will make things easier to understand.
At first in PART 1, we simply printed the outcomes
of simple expressions that make up
a somewhat complex statement.
The complexity is due to the fact mainly
that we're combining arithmetic and logical operator.
So the integerResult is seven
because a is larger than b in this case.
Let's continue.
In PART 2, we have a few things to look at.
First is we have the variable a
which printed value to be five.
Now, we are incrementing a to be six
multiplying by b which is negative three
to give us the result of negative 18.
Now we are printing a, and a is now six
because in line 44, we incremented a by one.
Use it in a calculation but after that it states six.
In PART 3, we are going to reset a
to its original value of five.
We, again, printing the value of a to be five.
We are using a in a multiplication times b.
However, in this case because the ++ sign is to the left,
we are using the original value of a
which is five times negative three, it's negative 15.
Now if we are printing the a after this operation,
a is now also the incremented by one, so in six.
So in both PART 2, a has been incremented by one.
The first time, it was incremented
prior to its use on line 44.
Where it's in line 52, it has incremented after its use.
In PART 4 and final part of this example,
we are going to reinitialize some of the values
and we're going to use the same complex statement
that we saw in our previous screen.
We are going to exemplify how if we multiply the counter
times an increment that added to the initialValue,
it will use different values of that counter.
So in line 60, we are incrementing the counter by one
but only after we are multiplying it by the increment.
So in line 62, the counter is now three times
the increment plus the initialValue and so on.
In line 64, we are first incrementing
the counter and then we are multiplying it.
With additional exercise and reading and trials
for experimentation, you will be able to figure out
on your own what is the best way of using and combining
expressions, operators, increments, and many others
as you will continue to increase your knowledge
about what's available in C.