# 13.2: Logical Operators

## Overview of the Logical Operators

Within most languages, expressions that yield Boolean data type values are divided into two groups. One group uses the relational operators within their expressions and the other group uses logical operators within their expressions.

The logical operators are often used to help create a test expression that controls program flow. This type of expression is also known as a Boolean expression because they create a Boolean answer or value when evaluated. The answers to Boolean expressions within the C++ programming language are a value of either 1 for true or 0 for false. There are three common logical operators that give a Boolean value by manipulating other Boolean operand(s). Operator symbols and/or names vary with different programming languages. The C++ programming language operators with their meanings are:

C++ Operator Meaning Comment Typing
&& Logical and   two ampersands
|| Logical or   two vertical dashes or piping symbols
! Logical not unary the exclamation point

The vertical dashes or piping symbol is found on the same key as the backslash \. You use the SHIFT key to get it. It is just above the Enter key on most keyboards. It may be a solid vertical line on some keyboards and show as a solid vertical line on some print fonts.

In most languages there are strict rules for forming proper logical expressions. An example is:

6 > 4 && 2 <= 14

This expression has two relational operators and one logical operator. Using the precedence of operator rules the two "relational comparison" operators will be done before the "logical and" operator. Thus:

1 && 1

or

true && true

The final evaluation of the expression is: 1 meaning true.

We can say this in English as: It is true that six is greater than four and that two is less than or equal to fourteen.

When forming logical expressions programmers often use parentheses (even when not technically needed) to make the logic of the expression very clear. Consider the above complex Boolean expression rewritten:

(6 > 4) && (2 <= 14)

## Truth Tables

A common way to show logical relationships is in truth tables.

 x y x && y false false false false true false true false false true true true
 x y x || y false false false false true true true false true true true true
 x !x false true true false

## Examples

I call this example of why I hate "and" and love "or".

Everyday as I came home from school on Monday through Thursday; I would ask my mother, "May I go outside and play?" She would answer, "If your room is clean and your homework is done then you may go outside and play." I learned to hate the word "and". I could manage to get one of the tasks done and have some time to play before dinner, but both of them… well, I hated "and".

On Friday my mother took a more relaxed view point and when asked if I could go outside and play she responded, "If your room is clean or your homework is done then you may go outside and play." I learned to clean my room quickly on Friday afternoon. Well needless to say, I loved "or".

For the next example, just imagine a teenager talking to their mother. During the conversation mom says, "After all, your Dad is reasonable!" The teenager says, "Reasonable. (short pause) Not."

Maybe college professors will think that all their students studied for the exam. Ha ha! Not. Well, I hope you get the point.

Questions
1. 25 < 7 || 15 > 36
2. 15 > 36 || 3 < 7
3. 14 > 7 && 5 <= 5
4. 4 > 3 && 17 <= 7
5. ! false
6. ! (13 != 7)
7. 9 != 7 && !0
8. 5 > && 7
1. 0
2. 1
3. 1
4. 0
5. 1
6. 0
7. 1
8. Error, there needs to be an operand between the operators > and &&.


## Demonstration Program in C++

#### Creating a Folder or Sub-Folder for Source Code Files

Depending on your compiler/IDE, you should decide where to download and store source code files for processing. Prudence dictates that you create these folders as needed prior to downloading source code files. A suggested sub-folder for the Bloodshed Dev-C++ 5 compiler/IDE might be named:

• Demo_Programs

If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.