Booleans

Basic Concept

Booleans either have a value of true or false
  • true: Represents a logically true condition.
  • false: Represents a logically false condition.
    A Boolean Expression is an expression that produces a Boolean (Either true or false) when evaluated
  • Common Boolean Expressions include equal to(=), greater than(>), less than(<), etc.
  • These are called Relational Operators
    Logical Operators allow you to combine multiple boolean expressions into one boolean output

Example 1

Create a program to check if a boolean is true. Create another to check if both are true or if either is true.

var variableA = true;
var variableB = false;

if (variableA == true) {
    console.log("VariableA is true!");
} else if (variableA == false) {
    console.log("VariableA is false!")
}

if (variableA == true && variableB == false) {
    "variableA is true and variableB is false"
}

if (variableA == true || variableB == true) {
    "Either variableA or variableB is true"
}
variableA = True
variableB = False

if variableA:
    print("VariableA is true!")
else:
    print("VariableA is false!")

if variableA and not variableB:
    print("VariableA is true and Variable B is false")

if variableA or varaibleB:
    print("Either variableA or variableB is true")    

Relational Operators

  • Equal to: a == b (True if a is equal to b)
  • Not Equal to: a != b (True if a is not equal to b)
  • Greater than: a > b (True if a is greater than b)
  • Less than: a < b (True if a is less than b)
  • Greater than or Equal to: a >= b (True if a is greater than or equal to b)
  • Less than or Equal to: a <= b (True if a is less than or equal to b)

Example 2

Create a boolean expression to check if a number is greater than 10.

if (number > 10) {
    console.log("number is greater than ten!");
} else {
    console.log("number is not greater than ten!")
}
if number > 10:
    print("number is greater than ten!")
else:
    print("number is not greater than ten!")

Logical Operators

NOT

  • NOT evaluates as true if the condition after not is false. If the condition is true, it evaluates as false.

    AND

  • AND evaluates as true if both conditions are true. If either or both conditions are false, it evaluates as false.

    OR

  • OR evaluates as true if either condition is true or if both conditions are true. If both are false, it evaluates as false.

Example 3

Create a boolean expression to check if a number is three digits.

if (number >= 100) and (number <= 999){
    console.log("number is three digits!");
}   else {
    console.log("number is not three digits!")
    }
if number >= 100 and number <= 999:
    print("number is three digits!")
else:
    print("number is not three digits!")

Homework Hacks

Review the instructions and examples in the lesson and create a truth table using Python. Additionally look up and learn about De Morgan’s Law and see how it can be used to create a boolean expression.

Here is an example:

function deMorganLaw() {
    console.log("A      B        Result");
    let values = [false, true];

    for (let A of values) {
        for (let B of values) {
            let result = !(A && B) === (!A || !B);
            console.log(`${A}  ${B}  ${result}`);
        }
    }
}

deMorganLaw();

Result:

A      B        Result
false  false    true
false  true     true
true   false    true
true   true     true

Take a quiz

Test out your skills with this quiz.

const link = document.createElement('a');
link.href = 'https://quizizz.com/join/quiz/66f58e14ced426df9b54f913/start?studentShare=true';
link.textContent = 'Quizizz Link';
document.body.appendChild(link);