Logic is one of the most important foundational concepts of CS as almost everything is based off of it. We use logic to create programs and build out ideas. In this article, we will take a look at how computers use logic to make decisions and how we can go about solving problems with it.
If we take a look at basic maths, you will see that we use variables and operators(+, -, =, x) to solve common mathematical problems. In mathematical logic these variables and operators represent the validity of things and can equate to either true or false.
For example, if we take the expression "IF the sea is cold, I won't swim" we can map it to logical variables A and B.
A: The sea is cold
B: I won't swim
A = True, means that the sea is cold
B =True, Means I won't swim
We can also use conditional operators, for example, A -> B. In this case the -> implies that if A = True then B = True as well.
If we made the following statement:
A -> B
Then, using the example above, we would be saying that if The Sea is Cold, I won't swim.
We can also express other ideas using different operators. For example there is a negation operator(!), this can be used to express the opposite.
!A means that A =False, therefore the Sea is warm.
!B means that B = False, therefore I will swim.
Given that A -> B and I swam, what can we say about the Sea? I cold sea forces me to swim so if I swim then it is impossible for the sea to be cold without swimming. For the sea to be warm every conditional statement has a contapositive equivalent: A -> B is the same as !B -> !A
Saying "IF the pool is cold, I'll swim" doesn't mean I'll only swill in cold water. The statement promises nothing about a cold sea, in other words, A -> B doesn't mean that B -> A. To express this condition we would need to use the biconditional operator.
A <-> B : I'll swim, IF and ONLY IF the sea is cold.
AND, OR, Exclusive OR
These logical operators are huge in programming and, for that reason, the most famous of the operators.
AND: Expresses that all ideas are true.
OR: Expresses that any idea is true.
EXCLUSIVE OR (XOR): expresses ideas of opposite truths.
Lets use an example of a party, where, heaven forbid, the only alcohol available is whisky and wine.
A = You drank wine
B = You drank whisky
A OR B = You Drank
A AND B = You Drank( You can expect a terrible morning in this case 😂)
A XOR B = You drank without mixing drinks.
Make sure that you understand these concepts, I have provided a couple of resources below to help you further understand the ideas expressed above.
Here is a useful link to a paper about logical operators.