- We are now proceeding with learning about
a different type of operator
which are called relational operators.
They are not very different
than what you are used to in algebra
but in C they take an additional meaning
as we will now see.
A relational operator it's also called the logical operator
because it enables you to make a logical inference.
For example when you look at group two,
you are asking is a value bigger than another value,
smaller, bigger or equal.
This is a result of a logical inference.
Those are the obvious one
and they should be known to you from algebra.
A slightly different type of operators
still in the same category of relational ones
are in group one.
The exclamation mark simply reverses
the logical value of an operator.
Meaning if it was true, it is now false.
If it was false, it is now true.
The logical equal simply asks
are two values identical?
Conversely if you combine the not with the equal sign,
you're asking are those values not equal.
Finally group three is the one that's most different
than what you are used to in algebra.
This one is called logical and and logical or.
You're essentially saying this,
if two conditions are both true
at the same time the outcome is true.
The logical end requires
that both items compared would be true.
The logical or will simply say
if one of those conditions is true,
then the result is true.
The best way to understand is
just to put it to work in a C program
as we will now see.
In this program we will take an in depth look
at each one of the relational and logical operators.
First, let's recall
that one is logical true
and zero is logical false.
Now we can proceed,
we are going to use two numbers.
They could be any numbers and they will suffice
for us to understand how this operator works.
Let's look at line 32,
X is three, Y is 12.
We are now asking the compiler
to evaluate the truth value
of the expression X equals Y.
We are actually asking is it true
that X equals Y,
this is what line 32 does.
It does actually more than that,
it looks at the expression
X equals Y
as being evaluated for a true or false value
and then it displays the result.
We could have done it in a separate step,
we could have collected that result
into another Boolean variable
and display the value of that Boolean variable.
This is an illustration of a shortcut
in how C can help you simplify the way you treat
relationships and expressions.
Line 33 does the same thing,
except now we are evaluating the truth value
of the expression X does not equal Y.
We are expecting that the result of that
to be true or one.
We can continue now.
In line 34 we are evaluating whether X
is bigger than Y then whether X is less than Y,
bigger than, less than and so on.
Let us run the program and we will look at
specific outcome of each one of those lines.
The first line is comparing X and Y
and the result is zero because it is false
to say that X equals Y.
The expression in parenthesis X equals Y
is evaluated as one result, one numeric result,
in this case Boolean and the outcome of it is zero.
We do not have to specify anywhere
that we are dealing with a Boolean or a logical inference.
The built in logical inference mechanism in C
displays a zero or an one to reflect
whether an expression is true or false.
Let's continue, for X equal three and Y equals 12,
it is true to say that they are not equal
therefore X does not equal Y,
yields the logical result one.
Three is definitely not bigger than 12
therefore on the next line it is false
to say that X is bigger than Y
but it is true to say that X is less than Y
as finally X is true
to say that it is less or equal to Y.
This conclude the first portion of this exercise
in which we are comparing the logical value
of operators that you have maybe become familiar with
in a previous algebra course.
Let's look at different ones and the one
that may seem a little bit different
when processed in C.
Line 42, we are asking to evaluate
the expression X and Y.
What this means is if X and Y
are both true then the result is true.
For this particular section,
I used different values of X and Y.
One and zero, one denotes true,
zero denotes false.
Therefore true and false cannot yield
a true result because both of them need to be true
in order for the total outcome to be true.
Indeed, when we ask C to evaluate X and Y,
the result is false.
When we ask to evaluate X or Y,
the result is true
because or simply requires
that one of the evaluated X or Y
to be true for the final result to be also true.
Finally we apply not to X.
Since X is one,
the inverse of X or the not value of X is zero.
This is not algebra, this is Boolean algebra
and it reflects the fact that C
is a low level language that also handles
assembly level bit operations
as we will see shortly.
Recall that zero and ones
are not just their arithmetical values
that you are accustomed to
but also denote a logical meaning.
A bit of one is true
and a bit of zero is false.
We will see more on the discussion of bits and bytes
as we progress and you will be exposed
to even more powerful language construct
and ability to calculate even more complex relationships
by mixing together arithmetic operations,
logical operators
and create very sophisticated inference engines.