DEV Community

IvanDev
IvanDev

Posted on

Foundations of Logical Thinking in Programming

One of the main concepts in programming is learning conditional logic or control flow logic.

Many programming tutorials often cover only the basics of this concept, focusing on the surface level.

In the following tutorial, you will explore essential mathematical concepts crucial for a deeper understanding of control flow logic.

You will become a much better programmer once you understand these concepts, whether a beginner or an expert.

Introduction to Logic Proposition

Logic

Humans' natural ability to think coherently and structure fully enables us to determine whether our reasoning is correct or incorrect.

  • i.e., If you don't know how to ride a bicycle, it's better not to ride it.

Math Logic

The study of the methods and essential principles used to distinguish between correct and incorrect reasoning.

  • i.e., If you got me flowers and candies, you only gave me candies.

Propositions

It's a logical proposition or statement that can be true or false.

  • Six is greater than 2. The statement is true or false; in this case, it is false.

Or it can be a question. Is six greater than 2? Those are the two ways to say that, which is a proposition we ask ourselves.

  • Colombia is on the American Continent. --- Statement: True or as a question: Is Colombia in the American Continent?

  • Number 8 is an even number --- Statement: True or as a question: Is eight an even number?

Those previous examples show that they were prepositions because we can tell that those statements have a test case to say True or False.

Now, if we take the following phrases, it can be said that:

  • What's your name? This is not a proposition because we can support it as True or False; it's just a question asking without further information.

  • Call your mother. This is a simple sentence, but it's not a proposition because we can't tell whether it's True or False; it's a sentence.

  • Wait for me. It's just a sentence telling something to a third person, but it is not a proposition because we don't know True or False.

How can we represent propositions more abstractly instead of telling long sentences then:

Those are the following lowercase letters to denote propositional variables:

  • p, q, and r… we can use as many as you can letters to represent propositional variables.

  • i.e.,

    • p: 2 is an integer
    • q: 3 x 2 = 2 x 3
    • r: All horses are black

In those previous cases, we assigned just the letters as propositional variables that we could use to perform logical operations.

Logical Connectors

It's two or more statements connected by denominated connectors. We called two or more statements connected as Compound Propositions.

Following are the logical connector symbols:

  • ¬, ~ No, or Negation
  • Ʌ Conjunction "AND"
  • V Disjunction "OR"
  • → Conditional "If/Then"

As we follow this tutorial, we will work with the following examples to understand better:

Negation “NO”

For negation, we will use ¬

Propositions:

  • p: I will go to a party.
  • q: I will play basketball.
  • r: I give you gift cards.
  • s: I give you candies.

If we want to negate a sentence, so we join the negation symbol with the letter variable like this case:

  • I won’t go to a party ¬ p
  • I won’t play basketball ¬ q
  • I don’t give you gift cards ¬ r
  • I don’t give you candies ¬ s

Conjunction Ʌ “AND”

  • I am going to a party, and then I will play basketball.
    • p Ʌ q
  • I give you gift cards and candies.
    • r Ʌ s

Disjunction V “OR”

  • I will go to a party or play basketball.
    • p V q
  • I give you gift cards or candies.
    • r V s

Conditional → If/Then

  • If I go to a party, then I will play basketball.

    • p → q
  • If I give you gift cards, then I will provide you with candies.

    • r → s

Practice

How do you read the following propositions:

  1. ¬ p Ʌ q
    • Solution: I won’t go to a party, and I will play basketball.
  2. p → ¬ q
    • Solution: If I go to the party, then I won’t play basketball.

Summary

In this first part, we introduced the basic logic, symbols, and formulas for writing propositions. You learned about logical propositions, their truth values, and how to represent them using logical connectors.

Conclusion

Understanding logical propositions and their applications in programming is fundamental for improving your problem-solving skills and mastering control flow logic. With a solid grasp of these concepts, you will be able to tackle more complex programming challenges. In the next part, we will delve into truth tables for the logical connectors, providing a practical foundation for applying these principles in your coding projects.

Top comments (0)