Fall 2024 - P5
Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
3.5 Booleans Relational Operators
Learn Relational Operators in Booleans
Booleans
- A Boolean value represents either true or false. When a Boolean expression is evaluated, it results in a Boolean Value(True or False)
Relational Operators
-
These are used to test the relationship between two variables. They are used for comparisons and result in a Boolean value of True or False.
- x == y (equals)
- x != y (not equal to)
- x > y (greater than)
- x < y (less than)
- x >= y (greater than or equal to)
- x <= y (less than or equal to)
Relational Operators Examples
Example 1: Test if one person’s age is greater than another
Python Version:
age_1 = 16
age_2 = 14
#Boolean expression:
is_older = age_1 > age_2
#Output:
if is_older:
print("Person 1 is older.")
elif age_1 < age_2:
print("Person 2 is older")
else:
print("Both people are the same age")
Javascript Version:
let age1 = 16;
let age2 = 14;
// Boolean expression:
let isOlder = age1 > age2;
// Output:
if (isOlder) {
console.log("Person 1 is older.");
} else if (age1 < age2) {
console.log("Person 2 is older.");
} else {
console.log("Both people are the same age.");
}
Popcorn Hack 1
Python Version:
temperature = 53
#Boolean expression:
cold = temperature < 65
#Output:
if cold:
print("It is cold.")
else:
print("It is not cold.")
Javascript Version:
let temperature = 53;
// Boolean expression:
let cold = temperature < 65;
// Output:
if (cold) {
console.log("It is cold.");
} else {
console.log("It is not cold.");
}