Coders: How to write complex “if” conditions by using logic



Learn the foundations of propositional logic and how you can apply them to write cleaner code

What you will learn

Understand the basic foundations of propositional logic

Know how to manipulate logic formulas involving and, or, not, implications, biconditionals and xor

Learn how to write complex if conditions to validate user inputs

Improve the way you code

Description

If you’re a self-taught software developer, you may have skipped all the mathematical mumbo jumbo and learned coding straight away. However, there are some mathematical tools, often taught in computer science courses, which can help you write nicer, cleaner code. One of them is propositional logic.

This mini-course teaches you the foundations of propositional logic in a practical way. You’ll see how this mathematical tool can be used to easily write and simplify complicated “if” conditions. This is particularly useful when validating a user’s input into a form or validating the body of an API request.

We’ll talk about:


  • How to translate our natural language into logic formulas;
  • How to manipulate logic formulas to make them simpler;
  • How to translate logic formulas into code.

Consider the following scenario: You need to write code to validate the user’s input. The input will pass the validation if the user has provided either an email address or a phone number, but not both at the same time, and, if the user has inputted their phone number, then they must also include the country dial-in code.

This kind of long-winded condition can be a bit confusing. In this mini-course, you’ll see how propositional logic can help you easily come up with the correct code for it.

English
language

Content

Introduction

Introduction
Why learn propositional logic

AND and OR

Propositions
Not
And
Multiple ands
And: Useful properties
Or
Multiple ors
Or: Useful properties
Combining Not with And and OR
Simplifying Not of And/Or
Trick: Detecting whether something is invalid
Combining And with Or
Factoring out
Excercise: And, or, and not

Implications

Implication
Chain of implications
IMPORTANT: How to get rid of implications

Putting it all together

Examples
Chain of implications revisited
Another example
Tautologies and contradictions
Exercise

More logical connectives

Biconditional
Xor

From logic to code

Propositions
Not
Or
And
Two equivalent programs
Implication
Biconditional
Xor
Exercise

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock

Check Today's 30+ Free Courses on Telegram!

X