Lesson 2.4: Combine Rules with And, Or, and Not
Boolean operators combine or change conditions.
andis true only when both conditions are true.oris true when at least one condition is true.notchanges true to false and false to true.
The condition weight < 10 and ready = true requires both rules. The condition color = red or color = blue accepts either listed color.
Keep combined rules readable. If a condition becomes hard to explain, store part of it in a clearly named Boolean variable, such as correctSize.
Guided practice
Set weight to 8 and labelReady to true. Print the result of weight < 10 and labelReady. Test again with weight 12, then with labelReady false.
Build a second condition that accepts a package when its code is A or B. Use not to print whether a package is not accepted. Make a truth table with your inputs and outputs before running the tests.
Loading the block workspace...
Ready to move on?
Only mark complete if you genuinely understand the material. Your progress will be saved in this browser.
Stuck on this lesson?
Ask about anything on this page. It can see which lesson you have open and which part you are reading.