Part II: Foundations of Logical Thinking in Programming, Logical Connectors

IvanIvan
7 min read

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

  1. If I give you cupcakes "and" gift cards, the result is "True."

  2. If I don't give you cupcakes, I give you gift cards. The whole phrase will be "False."

  3. If I give you cupcakes and don't give you gift cards. The entire phrase again is "False."

  4. 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"

pqp ∧ q
VVV
VFF
FVF
FFF

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:

  1. If I give you cupcakes "or" gift cards. The result will be "True".

  2. 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.)

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

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

pqp V q
VVV
VFV
FVV
FFF

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

  1. If I'm hungry(it's true) but I'm not hungry. This will a false statement, so the result will be false.

  2. 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
VFV
FVF

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)

pqp → q
VVV
VFF
FVV
FFV

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

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

  2. 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."

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

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

  1. Applying principles of propositions and building true tables.

  2. Conjunction tables logic "AND."

  3. Disjunction table logic "OR."

  4. Negation table NOT.

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

0
Subscribe to my newsletter

Read articles from Ivan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ivan
Ivan

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.