3.5 โ€” Boolean Expressions & Logic

Booleans, relational operators, modulus, and logical operators (NOT, AND, OR).

Boolean Values

Booleans represent two states: True or False. They often come from evaluating expressions (e.g., 5 < 10 โ†’ True) and are used in if-statements, loops, and decision making.

myHairIsBlack = True
iHaveADog = False

Key takeaway: Booleans let a program decide what to do based on conditions.

Relational Operators

== or === (in javascript)  Equal           (5 == 5 or 5===5)  โ†’ True
!=   Not equal       (4 != 5)  โ†’ True
>    Greater than    (7 > 3)   โ†’ True
<    Less than       (2 < 8)   โ†’ True
>=   Greater or equal (6 >= 6) โ†’ True
<=   Less or equal    (9 <= 4) โ†’ False

College Board expectations: recognize/apply relational operators, predict Boolean results, and use them within if-statements and algorithms.

Modulus Operator (%)

Finds the remainder after division โ€” great for even/odd tests and cycles.

How The Modulus Operator Works

How The Modulus Operator Works

We want to calculate: 10 % 3.5

  1. Divide 10 by 3.5: 10 รท 3.5 = 2.857...
  2. Take the integer part (floor): 2
  3. Multiply by 3.5: 2 ร— 3.5 = 7
  4. Subtract from 10 to get remainder: 10 - 7 = 3

โœ… Therefore, 10 % 3.5 = 3

Function of Modulus Operator: checking if a number is even or odd

The modulus operator % helps determine if a number is even or odd.

Directions:

  1. Take the number you want to check.
  2. Divide the number by 2 and find the remainder (% 2).
  3. If the remainder is 0 โ†’ the number is even.
  4. If the remainder is 1 โ†’ the number is odd.

Example: 7 % 2 = 1 โ†’ odd, 4 % 2 = 0 โ†’ even.

Logical Operators

Logical Operators

NOT (Negation)

Reverses the value of a Boolean. True becomes False, and False becomes True.

isCloudy = True
print(not isCloudy)  # False

AND (Conjunction)

Returns True only if both conditions are True; otherwise returns False.

didHomework = True
scored80 = True
didHomework and scored80  # True

scored80 = False
didHomework and scored80  # False

OR (Disjunction)

Returns True if at least one condition is True; otherwise returns False.

isWeekend = True
hasHoliday = False
isWeekend or hasHoliday  # True

isWeekend = False
hasHoliday = False
isWeekend or hasHoliday  # False

Key Points:

  • Use not to flip a Boolean.
  • Use and to require multiple conditions to be True.
  • Use or when only one of multiple conditions needs to be True.
  • Often used in if statements, loops, and filters.