What is a logic, and how do you use it?
A statement is a sentence that is true or false; however, a statement cannot be true and false. Lets look at an example of a statement:
2+2=4 and 2+5=6
are both statements, but one is true (2+2=4) and the other false. The sentence "Amir is a student" is a statement because this sentence can be true or false based on what we know about Amir. But the sentence "he is a student" is not a statement. This is because if we chose someone who is a student to be "he", then the sentence is true, but if we chose someone who is not a student to be "he" then the statement is false. Based on our definition of what a statement is, it is either true or false.
This example gives us a very interesting insight into what a statement is, that is all statements are sentences, but not all sentences are statements.
Question: Is "x + y > 0" a statement? (here x and y are any integer number we choose)
Answer: This is not a statement because we can say the following: x=1, and y = 1 so 1+1=2 and 2>0, which is true. But we can also say that x=-1, y=-1 and so -1+-1= -2, and so -2>0 is false. This is false meaning that the sentence "x plus y is greater than 2" is false and true. This shows that "x+y>0" is not a statement.
Now that we have looked at what statements are, we can apply our understanding of statement. For this we need to look at the types of operations we can do with statements. In this lesson we will cover three major ones. Much like when we are adding or subtracting numbers we can manipulate statements as well.
The Unary Operator
When we say unary it means only one thing. For example there is only one president, so there is a unary presedent. An operator is an action we perform on a statement. So when we say unary operator what we are saying is that there is an operator that only operates on one statement. We call this operator the not operator.
If we have a true statement for example $5=5$ then the not operator will reverse the value of the statement. So if $5=5$ is true then not $5=5$ is false. This means that the opposite of a true statement is a false statement, and the opposite of false statement is a true statement.
Lets summarize what a not operator can do. If we have a general statement $P$. It is either true or false and if we use our not operator then we can reverse their meaning our statement $P$ to false or true respectively.
We can use a table to show this more clearly:
The BINARY Operators
The word binary means 2, so in this case we are looking at operators that work with two statements. So if I have our first statement $P$ and our second statement $Q$ we can manipulate it so that we get an ouptput we want.
Before we look at the binary operators, lets first look at the different possible ways we can structure our statements. For examples if we have two statement then, our $P$ can be true or false. That is two possible values. Our $Q$ can also be either true or false. This means that there are a total of $2 \cdot 2 = 4$ possible values our output could have. This is what we would get:
The And Operator
The and operator is a definition that mathematiciens have come op with so that we can do useful math. In order to understand it we have to look at how we use "and" in our daily lives. Lets say that I have two statement: Amir bought an apple, Amir bought an orange. How would the meaning of our statements change if we said, 'Amir bought an apple "and" an orange'?
The "and" would imply that Amir bought two things, an apple and an orange. Lets say that Amir did indeed buy an apple, but he did not buy an orange. How would that change our statement?
Since Amir only bought one thing (the apple) it would mean that our statement that he bought an apple and an orange is false. We can say the same would apply if he did not buy an apple and bought an orange. Our statement would be false also when he did not buy an apple or an orange.
In summary, the and operator is going to be true if he bought both the orange and the apple; hence, we have our definition for the and operator. The result and operator is true only when two statemets are true. In all other cases the and operator is false. Here is a table for clarification:
|$P$||$Q$||$P$ and $Q$|
The OR operator
The or operator is similar to the and operator in that it is a binary operator, but its definition is different. The only time the or operator is false is when both statemetns $P$ and $Q$ are false. As an excercise try to figure out why mathmaticiens would want to make the or operator fit that definition.
|$P$||$Q$||$P$ or $Q$|
Lets look at examples of formal logic in programming. We will use the programming language python to look at conditional statements. As you can see below we have our "if" condition and our statement right after it. The "and" operator will check both of the statements while the "not" operator will invert true and false statements.
if 5 > 3: print("true: 5 is bigger") if 5 > 4: print("true: 5 is bigger") if 5 > 3 and 5 > 4: print("true: 5 is bigger than 3 and four") if not 5>9: print("false: 5 is smaller than 9")