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!")