Boolean Expressions and Comparison Operators
Expressions that come out true or false: comparisons between values, and the way and, or and not combine them.
What a learner can do afterwards
- Write a condition that is true only inside a range of values
- Predict the value of a condition that joins two comparisons with and or or
- Rewrite a condition using not without changing what it means
1 · Read
Programs ask questions that answer true or false, like score > 10 or temperature < 32. A snow jacket warning runs when temperature < 32 is true, and stays quiet when it is false. Every decision in code rests on one of these answers.
To catch numbers from 5 to 9 you write n >= 5 and n <= 9. The word and needs both sides true, while or needs only one side true. With n at 7 the whole test is true, and with n at 12 the first half fails so the test is false.
The word not flips an answer, and two nots cancel back out. So not age < 13 means exactly the same as age >= 13. Swapping an operator plus not keeps the meaning while making the code easier to read.
Read each condition aloud with real values plugged in. Also remember that one equals sign stores a value while two compare values, so mixing them up breaks your logic quietly.
Comparisons answer true or false, and and, or and not join them into bigger tests.
2 · Watch
Take it off screen
Where it sits
8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.