There are times when I find it hard to write a conditional and get the right result, so I decided to make some research and find out more about this fancy Boolean Algebra.
There is no rocket science, only basics. Hope to help some beginners to find the light at the end of tunnel. 😄
There is a kind of cheat sheet structured by levels with just a little theory.
❗ DISCLAIMER: This information is quite new for me as well, I encourage you to double check everything.
There is a data type called bool or boolean in computer
science with 2 possible values => True (1) and False (0)
Also, there are some boolean operators that are used in
conditional statements: Not (!), AND (&&), OR (||), XOR (^)
|x||y||x AND y||x OR y||x XOR y|
!True == False (NOT True equals False) !False == True (NOT False equal True)
Some simple rules of boolean algebra (x is "something"): False OR x == x True OR x == True False AND x == False True AND x == x
For the rest of the examples I have used short operators
(&& and ||) and constants A, B, C (these could be either true
A && A == A A || A == A A || !A == True A && !A == False
!=> high precedence,
&&=> medium precedence
||=> low precedence
!!A = A A && (A || B) == A A || A && B == A (A && B) && C == (A && B) && C == A && B && C (A || B) || C == (A || B) || C == A || B || C A && B == B && A A || B == B || A A && (B || C) == A && B || A && C A || B && C == (A || B) && (A|| C) A && (B || C) == (A && B) || (A && C) A || (B && C) == (A || B) && (B || C)
All of the above have some fancy law names, but I don't think that you'll ever need to know them. 😃
Level 4 (De Morgan's laws):
The name of this laws is too cool not to write it down 😁
!(A || B) == !A && !B !(A && B) == !A || !B
Thank you! 🙏
Latest comments (2)
In "Level 3", where you have
A && B == B && A(and same for
And yes; DeMorgan's Law is super cool!
Thank you, Nathan! Very good point 👍