Truth tables and conditional statements in programming
In mathematics, there is a term called two-valued logic. It states that every statement is either True or False, and none is both. The two-valued logic supports computer logic in that one can decide about every preposition.
The entire computer systems and operations depend on logic influenced by conditional statements. A statement evaluated as True or False is the simplest, most minor decision a program can make. It takes into account two or more factors and considers them to be either one or the other.
True or False evaluations are so critical in programming that they have their own datatype - the boolean datatype.
Booleans
A boolean is a binary data type that evaluates to either True or False. Boolean is named after a British mathematician, George Boole, the formulator of the boolean algebra. It is the foundation and simplest form of modern programming logic.
In Python, the boolean class is called ‘bool’.
x = True
if x:
print(f'{x} is true')
print(type(x))
#<class 'bool'>
Truth Table
A truth table is a table that displays the output of a combination of logical operations that evaluates to either True or False.
Truth tables are crucial because they determine the evaluation of logical expressions and show logical relationships.
On its official document on truth value testing, Python mentions the initial state of an object and how it can change to false. “By default, an object is considered true unless its class defines either a __bool__() method that returns False or a __len__() method that returns zero, when called with the object.”
Which values do we use for a truth table, and how do we use them? Python documentation also states that “Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations”. The boolean operations here refer to the logical operators.
Comparison Operators
Boolean expressions require a comparison operator to compare the expressions to be evaluated. There are eight comparison operators in Python. The operators perform the same function for a truth table as they would other conditional statements.
Operator | Meaning |
---|---|
< | Less than |
> | Greater than |
<= | Less than or equal to |
>= | Greater than or equal to |
== | Equal to |
!= | Not equal to |
Is | Object identity |
Is not | Negated object identity |
age = 25
if age > 19 and age <= 29:
print('You are in your twenties')
else:
print('You are not in your twenties')
Logical Operators
A logical operator is a word or symbol that evaluates two or more Boolean expressions to be either a true or false value. The result of the statement is dependent on the type of logical operator used. Generally, in programming, there are three major types of logical operators. AND, OR, and NOT. A language like FORTRAN takes five. NOT, AND, OR, EQV, and NEQV. The last two stand for logical equivalence and logical not equivalence respectively.
Each logical operator could be the exact keyword or a symbol substitute. This condition varies from program to program.
NB: For this article, we will write sample code in Python.
AND Operators
AND operator takes a strict approach to evaluate boolean expressions. If either one is false, then it evaluates to false. It only gives a true output when all the expressions are true.
Python uses a short circuit evaluation style to evaluate the expressions. A quick circuit evaluation does not necessarily check all the operands or expressions. For AND, once Python evaluates the first expression as false, it doesn’t bother checking the second one. The output becomes false. However, if the first output is true, it evaluates the second (and third, depending on how many expressions there are). Once it finds a false, it gives its output as false; else, it returns true. This condition is displayed in a truth table like this:
A | B | A and B |
---|---|---|
False | False | False |
True | False | False |
False | True | False |
True | True | True |
Let us look at a practical situation that will help us write a program that describes how AND works. Due to ongoing COVID issues, strict rules and general guidelines govern how we do activities involving a large crowd. To host a party or an award, attendees must vaccinate AND wear a mask. Neither of these is optional; they must both be true.
#example of how AND works
#program to determine if an attendee should be let into a party
name = 'Mr. Tim'
wears_a_mask = False
vaccinated = True
if wears_a_mask and vaccinated:
print(f'{name}, you may enter!')
else:
print(f'{name}, you have to stay out!')
# Mr. Tim, you have to stay out!
Mr. Tim is vaccinated, but because he doesn’t wear a mask, he cannot enter.
#another example
x = 5
if x < 1 or x < 10:
print('X is cool')
else:
print('X is not cool')
OR Operators
OR logical operator takes a more relaxed approach while evaluating boolean expressions. If either one of the expressions is true, then it evaluates to true. It also uses a short circuit evaluation method. Once the first expression is true, it doesn’t bother checking the rest and simply returns a true. But if the first is false, it runs through the others until it finds a true and returns it, else it returns false. This condition is represented on a truth table:
A | B | A or B |
---|---|---|
False | False | False |
True | False | True |
False | True | True |
True | True | True |
Not every public place requires stringent rules. Some areas cater to very few people at a time with some form of social distancing. They need a person to either be vaccinated or wear a mask. Both do not have to be true.
Suppose Mr. Tim walks into a restaurant in his neighborhood, wears a mask but is not vaccinated. Then:
#example how OR works
#program to determine if an attendee should be let into a party
name = 'Mr. Tim'
wears_a_mask = True
vaccinated = False
if wears_a_mask or vaccinated:
print(f'{name}, you may enter!')
else:
print(f'{name}, you have to stay out!')
# Mr. Tim, you may enter
x = 5
if x < 1 or x < 10:
print('X is cool')
else:
print('X is not cool')
NOT Operators
NOT logical operator negates a boolean value. It returns a true when the expression is false and vice versa. If a value is false, the NOT value becomes true, and the program executes. If a value is true, the NOT value becomes false, and the program does not run. Here is how this condition looks in a table format:
A | Not a |
---|---|
True | False |
False | True |
Unlike AND and OR, NOT operators do not need to evaluate two expressions. It negates the same expression.
c = 5
if not c > 5:
print('Returns True')
else:
print('Returns false')
#returns true
c = 5
if not c == 5:
print('Returns True')
else:
print('Returns false')
#returns false
Combining two or more logical operations
In its simplest form, we have looked at the basic logical operators and how they work. We can combine logical statements in different ways to generate even more complex tables that could take as much as 5 to 7 inputs.
(A and B or C) – Again, here is this logic in a table format:
A | B | C | X |
---|---|---|---|
True | True | True | True |
True | True | False | True |
True | False | True | True |
True | False | False | False |
False | True | True | True |
False | True | False | False |
False | False | True | True |
False | False | False | False |
Using Python, we could generate a table that could get the same output by setting up an array of a combination of possible inputs represented by zeros and ones, where 1 represents True and 0 False.
os = [[0,0,0], [0,0,1],[0,1,0],[0,1,1], [1,0,0], [1,0,1],[1,1,0],[1,1,1]]
print('A\t\tB\t\tC\t\tX')
for truth_value in nos:
if truth_value[0] == 1:
a = True
else:
a = False
if truth_value[1] == 1:
b = True
else:
b = False
if truth_value[2] == 1:
c = True
else:
c = False
print(a, "\t",b, "\t",c, "\t", ((a and b) or c))
#output
A B C X
False False False False
False False True True
False True False False
False True True True
True False False False
True False True True
True True False True
True True True True
Conditional variations and their equivalents
Conditional statements are not only the statements that we can compile on a truth table. Logical statements have different conditional variations. They are converse, inverse, contrapositive, and we can represent these statements on a truth table alongside the conditional statement:
A | b | Not a | Not b | If a then b (conditional) | If b then a (converse) | If not a, then not a (inverse) | If not b, then not a (contrapositive) |
---|---|---|---|---|---|---|---|
True | True | False | False | True | True | True | True |
True | False | False | True | False | True | True | False |
False | True | True | False | True | False | False | True |
False | False | True | True | True | True | True | True |
The equivalent of a conditional variation is the one that shares the same truth table as them.
Here are a few examples of conditional, inverse, converse, and contrapositive statements:
Conditional:
If I pass my high school final exams, then I will apply for college
Converse:
If I apply for college, then I will pass my high school final
Inverse:
If I do not pass my high school exams, then I will not apply for college - inverse
Contrapositive
If I do not apply for college, then I will not pass my high school exams
Conclusion
Truth tables are not very popular in mainstream programming, but they are tied to logic and conditions, which form the core of programming. It makes them a special skill to have.
To learn more special skills in programming languages, please check our dedicated article section for developers.