Sunday, December 7, 2014

Logical Operators, and Relational Operators

We have discussed bitwise operators and how they are used to change the status of bits.  Logical and Relational operators are all binary operators in that they operate on two operands and return a result, but unlike bitwise operators these deal entirely in comparing the two operands to return a true or false result.

LOGICAL OPERATORS

Figure 1.
The logical operators are shown in figure 1.  These can be confusing to beginners because their names are the same as two of the bitwise operators, and even the symbols are similar.  Looking at examples makes their differences easier.  A logical AND operation evaluates two operands and says "If operand 1 AND operand two are true, the result is true, otherwise the result is false.  Both operands must be true to return a true result.  Here are some examples.  We have two variables that we are comparing, A and B.
A = 2, B = 5
(A && B) returns a true result, because both A and B are non-zero.  If B = 0, then (A && B) returns false because both operands are not true.
We can use this logical operator to influence program flow by asking the program to execute or not execute some instruction based on the condition of two operands.  For instance, if A is greater than zero AND B is greater than zero, perform this task.

A logical OR performs in much the same way except that it says if A OR B is true, the result is true.  Both operands do not need to be true to return true, only one of them does.  However, if neither are true, the result is false.
A = 2, B = 0:  A || B equals true
A = 0, B = 1: A || B equals true
A = 0, B = 0: A || B equals false

RELATIONAL OPERATORS

We have seen relational operators already in these lessons, but it's time we define them.  Figure 2 shows the relational operators, which compare the relationship of two operands and produces a true or false result based on the relational operator being used.
Figure 2.
I'm sure many of these operators look familiar to you since they are almost all used in everyday math, greater than and less than, for instance.  When we are using relational operators, we are comparing the two operands based on their relationship to each other.  Is X greater than Y?  Is Y equal to X?

Two of the relational operators that you may not be familiar with are "is equal to" and "is not equal to".  Notice how the relational operator "is equal to" uses two equal signs ==, it is dangerously close to the operator "make equal to", which uses only one equals sign.  Here is the difference:
A = B changes the value of A to equal the value of B. If B were equal to 5, this operator would make A equal to 5.
A == B produces a true or false result depending on whether A and B have the same value. If A = 2 and B = 5, then A == B is false because A is not equal to B.  If A = 1 and B = 1, then A == B is true.

Is not equal to (!=) generates a true or false result based on whether the operands are different. If A = 2, B = 5, then A != B returns true because A is not equal to B.

EXAMPLE

Let's look at a couple examples to understand these operators better.  These examples will use what is called "pseudo code" meaning that it's not legitimate code, but more like paraphrasing the code to get the point across.

Assume we have a button connected to a pin, and a LED connected.  Let's use a variable "count" to count up from 0 to 10.

unsigned char count = 0;   //this operator makes "count" equal to 0


while (1)
    if (button && count)
         turn on LED;
    count ++;

  if (count >= 10)
    count = 0;

This program would turn the LED on only if the button is pressed (making the button pin a non-zero) and count is not equal to 0.  If the button were held down, the LED would only be off when the count variable passes through 0. Then count gets incremented using count ++. Then we use a relational operator so that the value of count cannot exceed a value of 10. This is accomplished by saying "if count is greater than or equal to 10, make count equal to 0".  As long as count is less than 10, the count = 0 instruction gets skipped.

If we change the logical operator AND (&&) to OR (||):

if (button || count)

Then the LED is only off if the button is not pressed and count is equal to 0, because we are saying if button is true OR count is true.  If the button was continuously held in this example, the LED would never turn off because button is always true.

We will see logical and relational operators used in practical examples soon.

No comments:

Post a Comment