Programming Fundamentals/Logical Operators

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Overview[edit | edit source]

A logical operator is a symbol or word used to connect two or more expressions such that the value of the compound expression produced depends only on that of the original expressions and on the meaning of the operator.[1] Common logical operators include AND, OR, and NOT.

Discussion[edit | edit source]

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. 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:

Language AND OR NOT
C++ && || !
C# && || !
Java && || !
JavaScript && || !
Python and or not
Swift && || !

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

6 > 4 and 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:

true && true

True and True

The final evaluation of the expression is: 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)

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

Most programming languages recognize any non-zero value as true. This makes the following a valid expression:

6 > 4 && 8

6 > 4 and 8

But remember the order of operations. In English, this is six is greater than four and eight is not zero. Thus:

true && true

True and True

To compare 6 to both 4 and 8 would instead be written as:

6 > 4 && 6 > 8

6 > 4 and 6 > 8

This would evaluate to false as:

true && false

True and False

Truth Tables[edit | edit source]

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

Logical AND
x y x and y
false false false
false true false
true false false
true true true
Logical OR
x y x or y
false false false
false true true
true false true
true true true
Logical NOT
x not x
false true
true false

Examples[edit | edit source]

Take a look at the following hypothetical scenarios:

AND:

A mother tells her son, he must clean his room AND do his homework to go outside. If the son only does one of the tasks his mother asks of him, he will not be allowed outside because he has failed to complete the other task. 

Remember from this lesson that when using an 'AND' statement the same logic applies, the program will not run the statement associated with the true condition, unless all conditions in an 'AND' statement are true.

OR:

Karen is told she can get an 'A' if she writes an essay OR successfully hacks into the class gradebook. By saying OR the teacher has given Karen two possible ways to get an 'A'. Each way is entirely independent from the other so she does not have to do both tasks to get an 'A' only one. 

Remember from this lesson that when using an 'OR' statement only one of the conditions listed in the statement has to be true in order to get the statement associated with the true condition to run.

NOT:

Mark is told he can only go to the club if he's NOT Steve. This is due to Steve being banned from the club for rowdy behavior. While Mark and Steve look a like, they are not the same person. Mark proves this by showing the bouncer his 'I.D.'. Since Mark is NOT Steve the bouncer lets him in and for the rest of the night he has a great time.

Remember from this lesson that when using a 'NOT' statement the program will consider any value that is equal to the value of the listed statement as false. Only conditions that do 'NOT' equal, the statements value will run as true.


Makes sense right? Hopefully you can apply this to the following expression examples listed below.

Examples:

  • 25 < 7 || 15 > 36
  • 15 > 36 || 3 < 7
  • 14 > 7 && 5 <= 5
  • 4 > 3 && 17 <= 7
  • ! false
  • ! (13 != 7)
  • 9 != 7 && ! 0
  • 5 > 1 && 7

More examples:

  • 25 < 7 or 15 > 36
  • 15 > 36 or 3 < 7
  • 14 > 7 and 5 <= 5
  • 4 > 3 and 17 <= 7
  • not False
  • not (13 != 7)
  • 9 != 7 and not 0
  • 5 > 1 and 7


Key Terms[edit | edit source]

logical operator
An operator used to create complex Boolean expressions.
test expression
Also known as a Boolean expression.
truth tables
A common way to show logical relationships.

References[edit | edit source]