This content originally appeared on DEV Community and was authored by IvanDev
Logical Connectors
Introduction
If you haven't read the previous article If you haven't read the previous article, please read it first to understand the basic concepts quite well. With this second part, I can provide real-life examples of handling use cases using truth tables for basic logic, implementing basic sentences to convert in the conjunction math logic, and programming tables "AND."
Conjunction and in programming "AND"
For example, in writing sentences that have meaning, you have to emphasize what makes sense in real life so you can apply it in math logic:
p: I will give you cupcakes
q: I will provide you with gift cards
- If I give you cupcakes "and" gift cards, the result is "True."
- If I don't give you cupcakes, I give you gift cards. The whole phrase will be "False."
- If I give you cupcakes and don't give you gift cards. The entire phrase again is "False."
- If I don't give you cupcakes and gift cards. The reasonable result is "False," right?
In conclusion, conjunction: if one sentence is false, the final result will be false; otherwise, just one option will be true.
Now, I can apply all of the previous sentences in "true tables" like this:
I need first to calculate how to make the cells and columns to build the table with the following formula:
Since I have two sentences, p and q, then:
2^n where "n" to the power of the number of propositions
2^2= 4. In this case, we need four rows
Conjunction Table "AND"
When we use "AND," try to understand that if we have two options and one is false, the results will always be false. If both are true, of course, the result is correct. First, we need a row for headings, for a total of five rows.
Then, we follow the math logic but with True "T" and False "F" symbols and represent the sentences as symbols, too, "p" and "q."
I built like "p" column values, the second column for "q" values, and a third column for the join result of the columns "p" and "q" with the added conjunction symbol."∧" or "AND":
Conjunction Table "AND"
p | q | p ∧ q |
---|---|---|
V | V | V |
V | F | F |
F | V | F |
F | F | F |
Disjuction "OR" Table
As we did in the previous example with the Conjunction table, I will apply these principles to our "OR" table next. Remember that "OR" means that you can choose if you have two options, and the result will be more truthful than false.
First, let's build our sentences:
- If I give you cupcakes "or" gift cards. The result will be "True".
- I will give you cupcakes or (not give you) gift cards. The join result will be "True". Why? (Logic explanation: If either sentence is true, the entire phrase is true because we have two options to pick from.)
- If I don't give you cupcakes, "but" I give you gift cards. In this case, the "but" is like the "or" option, so if you pick between both and one, the result will indeed be true.
- If I don't give you cupcakes or gift cards, the entire phrase is false.
In conclusion, if just one sentence proposition is true, the result will be true, and in just one case, it is false where two sentences are false.
Now, let's build the Disjuction "OR" table:
Once again, we have two sentences, so use the formula 2^n. Where "n" is the number of sentence propositions.
2^2 = 4. Apply four rows to our table plus one row for the headings:
Disjunction "OR" Table
p | q | p V q |
---|---|---|
V | V | V |
V | F | V |
F | V | V |
F | F | F |
Negation Table
It's just to negate one sentence like this:
I'm hungry. Negation will be: I'm not hungry.
It's pretty straightforward to understand and do the opposite of the sentence. If it's positive, swapped to negative.
p: I'm hungry
¬ p: I'm not hungry
- If I'm hungry(it's true) but I'm not hungry. This will a false statement, so the result will be false.
- If I'm not hungry(it's a lie), but the truth is I'm hungry. The result in this case it's true because I'm hungry.
Note: This is the only logical connector that we can negative twice next keep on doing the next example:
¬ ¬ p or (¬ p):(that means two negations):
It's false that I'm not hungry
¬ ¬ p
Now, to get the result of the third column, we have to analyze the following:
If we have two negations in a row, those negatives are eliminated.
The previous compound propositions will be the initial proposition.
Let's show the example in the negation table:
To build the table, the same formula: 2^n. "n" power number proposition.
2^1 = 2. So we only need two rows.
¬, ~ No, or Negation
p | ¬p | ¬ ¬ p |
---|---|---|
V | F | V |
F | V | F |
As you can see, the initial value in the first row of column "p" is "V," so the result of that negation will be "V" and vice versa.
Check this example sentence:
It is not colorless, which means that it does indeed have some color. Why? because we eliminated two negations: "not" and "less." As a result, it has a "color."
The previous statement is hypothetical and might be true; this is just an example. Don't take it for granted; you have never heard that statement that often.
Conditional If/Then →
If we have two propositions to join and get a result, in other words, if you have a cause, then you have a consequence.
Let's see these two sentences examples:
p: You study every day
q: I will take you to travel
Now, let's convert the sentences into propositions:
If you study daily, I will take you on a trip.
As you remember, we have to build a truth table. Let's do it now:
Since we have to sentences apply the formula:
2^2 = 4. We get four rows plus one row for the headings and three columns: "p", "q" and "p → q"(conditionals)
p | q | p → q |
---|---|---|
V | V | V |
V | F | F |
F | V | V |
F | F | V |
Note: If the result is false, it's because the first value is True, and the second value is False.
Analyze the four cases of the table based on the initial propositions statements:
"If you study daily, I will take you on a trip."
- If you study daily, then I will take you on a trip. The result is "True." The explanation is that if you study daily, I will give you the promised trip.
- I studied daily but didn't give him the promised trip. Explanation: I didn't comply, "but" (it's like then) it works like his request to go on a trip. In this case, the entire proposition will be "False."
- I didn't study, and then he took me on a trip. Here, we see that the first sentence is false, but it was recognized with the trip. In this case, the result is "True". It finally has the trip.
- I didn't study, and you didn't take a trip. Finally, we have the two sentences that are false, so the result is "False."
Summary
- Applying principles of propositions and building true tables.
- Conjunction tables logic "AND."
- Disjunction table logic "OR."
- Negation table NOT.
- Basic principles use conditional If/Then in building compound propositions to build a true compound conditional table.
Conclusion
With this second part of the math logic series, you will have a more robust knowledge of the math logic involved that you might encounter in basic programming principles. Please take your time to do more exercises. Once you master the key components and how to build sentences and transform them into tables, the rest of the logic will be more straightforward.
About the Author
Ivan Duarte is a backend developer with experience working freelance. He is passionate about web development and artificial intelligence and enjoys sharing their knowledge through tutorials and articles. Follow me on X, Github, and LinkedIn for more insights and updates.
This content originally appeared on DEV Community and was authored by IvanDev
IvanDev | Sciencx (2024-08-20T19:36:47+00:00) Part II: Foundations of Logical Thinking in Programming, Logical Connectors. Retrieved from https://www.scien.cx/2024/08/20/part-ii-foundations-of-logical-thinking-in-programming-logical-connectors/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.