Section 6c

Boolean Combinations and Logic Operators


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Continuing with Logical Operators

There are three pairs of operators that help make your logical choices a little easier to analyze. They are shown here.

OPERATOR

MEANING

&&

AND

||

OR

!

NOT

The AND logical rule

The AND operator requires that any two or more quantities must ALL be true for the whole expression to evaluate to true.

A simple example is shown here.

bool a, b, c;
a = true;
b = true;
c = ( a && b );

In the example above, the expression between the parentheses evaluates to true, so the variable c now holds the value true. On the other hand, the following shows when an expression will not evaluate to true.

bool a, b, c;
a = true;
b = false;
c = ( a && b );

In the example above, the expression between the parentheses evaluates to false, so the bool variable c now holds the value false. The AND operator requires that both (or all, if more than two) values must be true. Shown next is one more expression with more than two bool values.

bool a, b, c, d, e, f;
a = true;
b = false;
c = true;
d = true;
e = true;
f = ( a && b && c && d && e );

In this last example, as you can see, the bool variable f will hold the value false since one of the bool terms in the expression, b, is false.

The OR logical rule

The OR operator requires that AT LEAST ONE of the two or more terms be true for the expression to evaluate to true.

Here is an example.

bool a, b, c;
a = false;
b = true;
c = ( a || b );

In the example above, the variable c now holds the value true. On the other hand, the following shows when an expression will not evaluate to true.

bool a, b, c;
a = false;
b = false;
c = ( a || b );

Once again in the example above, the bool variable c holds the value false. In addition, if an expression has many terms, it only takes one to make the whole expression evaluate to true. This is demonstrated here.

bool a, b, c, d, e, f;
a = false;
b = false;
c = true;
d = false;
e = false;
f = ( a || b || c || d || e );

Because one of the terms was true, the whole expression evaluated to true, and the bool variable f will now hold the value true.

The NOT logical rule

Logically speaking, and as mentioned previously, there are only two logical ways to see the world. A logical result can either be true or false. However, logically speaking again, the world can be made even simpler by saying that the two results of a logical operation can be true or not true (or false and not false). The NOT operator extends the flexibility of logical analyses so that expressions can be simplified, or made clearer in your programs.

As a matter of fact, the C programming language uses this in its analysis of logical comparisons. When an expression is tested with any of the relational operators, if the process yields a zero, the result is considered false. If the process yields a one (i.e., NOT zero), the result is considered to be true. It is legal to use the values zero and one as false and true respectively, but it is very poor computer programming. This was done back in the 1980s but that was before we elevated programming to a higher-level thinking process.

Once you have abstracted a quantity into a useful tool, such as developing a data type that holds true and false, you should stick with the abstraction. Going back and forth between the abstraction and the things it represents rapidly loses the higher level problem solving processes that you are reaching for.

Shown next are a few examples of using the NOT operator.

bool a, b, c;
a = false;
b = false;
c = !( a || b );

In the example above, the bool variable c will hold the value true. This is because the expression inside the parentheses evaluates to false (false OR false evaluates to false). Then the NOT operator changes the result to the opposite of what it was, which makes the expression result in true. Another example is shown below.

bool a, b, c;
a = false;
b = true;
c = !( a && b );

In the example above, the expression will also evaluate to true. The expression a AND b (e.g., false AND true) evaluates to false, and the NOT operator switches it to true.

One more example is shown below.

bool a, b, c;
a = true;
b = true;
c = !( a && b );

In the example above, the expression will evaluate to false. The expression a AND b (e.g., true AND true) evaluates to true, and the NOT operator switches it to false. In a nutshell, the NOT operator takes whatever expression or term it is in front of and switches it to the opposite of what it was.

The NOT operator can also be used with an equality test operation to emulate the not equal operator (i.e., "!=") you read about earlier in this topic. This is conducted as shown here.

bool testForNotBeingZero = !( x == 0 );

is the same as:

bool testForNotBeingZero = ( x != 0 );

While these are both exactly the same operation, there are times that one of them might be clearer to use than the other. As usual, watch for the opportunities to make your program more readable.

One more note about the use of parentheses. If there is ever a question about what you meant to do with an expression, use parentheses to show your intentions. There will be many times that they will not be necessary since the order of operations rules will take care of things for you. Remember (again) that you are not just writing a program to work; you are writing it to be reviewed, maintained, and potentially upgraded.

Using parentheses to make your intentions clear in a program is one of the easiest ways to minimize incorrect expression results. The computer will not make mistakes by implementing your expression differently than you meant to, and your fellow programmers will not make mistakes misinterpreting your code.

These first sections have been an introduction to both the concepts of Boolean expressions and the Boolean operators and tools you have to work with in the C programming language. Your next steps are to put these into action, first in a conceptual way, and then with code.