Bootcamp Curriculum • 5 Modules • 12 Chapters • Multi-Language Runner (Python, JS, Java, C++) • Interactive Memory Trace • AI Mentor
CHAPTER 05 · CONTROL-FLOW5 min read

Logical operators: and, or, not

Real-world rules often require checking multiple conditions at once: 'You must be at least 120cm tall AND have a ticket, OR be accompanied by an adult.' Logical operators make this possible.

By the end of this chapter, you can:
  • Combine conditions using the 'and' operator (both must be True).
  • Use the 'or' operator (at least one must be True).
  • Invert conditions cleanly using the 'not' operator.
Explanation DepthTailor the level of detail to your learning goal
💻 Standard College Notes

Boolean algebra with and, or, not, and operator precedence.

Logical operators combine multiple conditions. Precedence order: not takes precedence over and, which takes precedence over or. Use parentheses to eliminate ambiguity.

💡A and B is True only when both are True; A or B is True when at least one is True.

Roller-Coaster ticketing logic

You can only ride if height >= 120cm. Ticket price depends on age: child (<12: $5), youth (12-18: $7), adult (>18: $12). Plus an optional photo pass for $3.

example_1.py
Ctrl + ↵ to run
Terminal Output
Ready to execute
Click "▶ Run Code" or press Ctrl+Enter to compile & run this snippet.
✦ Interactive Dry-Run Visualizer

Memory Trace: Multi-Tier Condition Evaluation

Step through height validation, age price bracket, and photo surcharge.

STEP1 / 7
1height = 135👉 Executing
2age = 16
3bill = 0
4if height >= 120:
5 bill = 7
6bill = bill + 3
7print(bill)
VariableValue in MemoryData Type
height135int
Initialize rider height

135cm stored.

Console Stream (stdout)
(No output generated yet)
Line 1: height = 135

🔧 Missing Parentheses in Logic Precedence

A rollercoaster admission check let riders in without a pass because OR evaluated with unexpected precedence.

⚡ +100 XP
fix_bug.pyPython 3.10
⚡ Quick Revision Quiz

Predict The Output (2 Questions)

Question 1 of 2:
What does False or True and False evaluate to?
Question 2 of 2:
What is the output of: not (10 > 5)?

Your lightbulb moment

✧

The key takeaway
`and`, `or`, and `not` allow compounding logical conditions to model real-world business and game rules.

Make it yours

Try changing the age to 25 and rerun to verify that the adult tier ($12 + $3 = $15) runs automatically.

Try editing the code in the playground above in Python, JavaScript, Java, or C++, and use the step-by-step Dry-Run Table to inspect how variables mutate in memory. If you get stuck, click the 🤖 AI Doubt Solver button on your screen for instant explanations!