Boolean Logic & Branching
Programs need to make decisions. Boolean logic determines whether conditions are
True or False, and branching structures (if/elif/else) allow
the program to take different paths based on those conditions.
Learning Objectives
- 11.1.1.7 Apply logic operations to Boolean variables
- 11.1.1.8 Use logical operations AND, OR, NOT in selection structure
- 11.1.1.9 Implement the branching algorithm according to the flowchart
Conceptual Anchor
The Traffic Light Analogy
A traffic light is a branching algorithm: IF the light is green → go; ELIF yellow → slow down; ELSE (red) → stop. The light's colour is a condition that evaluates to True or False for each check. Logical operators combine conditions: you cross AND the light is green AND no pedestrians are crossing.
Rules & Theory
Comparison Operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
== |
Equal to | 5 == 5 |
True |
!= |
Not equal to | 5 != 3 |
True |
> |
Greater than | 5 > 3 |
True |
< |
Less than | 5 < 3 |
False |
>= |
Greater or equal | 5 >= 5 |
True |
<= |
Less or equal | 5 <= 3 |
False |
Logical Operators: AND, OR, NOT
| Operator | Meaning | True when… | Example |
|---|---|---|---|
and |
Both conditions must be True | A is True AND B is True | (x > 0) and (x < 100) |
or |
At least one condition is True | A is True OR B is True (or both) | (x == 0) or (x == 1) |
not |
Inverts the value | A is False | not (x > 10) |
Truth Tables
| A | B | A and B | A or B | not A |
|---|---|---|---|---|
| True | True | True | True | False |
| True | False | False | True | False |
| False | True | False | True | True |
| False | False | False | False | True |
if / elif / else Structure
# Pattern: if → elif → else
if condition_1:
# executes when condition_1 is True
elif condition_2:
# executes when condition_1 is False AND condition_2 is True
else:
# executes when ALL above conditions are False
# Example: Grade classifier
score = int(input("Enter score: "))
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
elif score >= 60:
print("Grade: D")
else:
print("Grade: F")Flowchart → Code
A flowchart diamond (decision box) = if condition. The
Yes arrow = the indented block. The No arrow =
elif or else. Always trace the flowchart path from top to bottom,
matching each diamond to a condition in code.
Worked Examples
1 Boolean Variables
# Boolean variables hold True/False
is_raining = True
has_umbrella = False
# Logical expressions
can_go_outside = not is_raining or has_umbrella
print(can_go_outside) # False
# Explanation:
# not True → False
# False or False → False2 Compound Conditions with AND/OR
age = int(input("Enter age: "))
has_id = input("Do you have ID? (yes/no): ") == "yes"
# AND — both must be true
if age >= 18 and has_id:
print("Access granted")
else:
print("Access denied")
# OR — either one is enough
day = input("Enter day: ")
if day == "Saturday" or day == "Sunday":
print("Weekend!")
else:
print("Weekday")3 Nested if — Cinema Ticket Pricing
age = int(input("Enter age: "))
is_student = input("Student? (yes/no): ") == "yes"
if age < 12:
price = 500
elif age >= 12 and age < 18:
if is_student:
price = 700
else:
price = 900
else:
price = 1200
print("Ticket price:", price, "tenge")4 Flowchart Implementation — Number Classifier
Flowchart: Input number → Is it positive? → If yes: Is it even? → Print result
num = int(input("Enter a number: "))
if num > 0:
if num % 2 == 0:
print(num, "is positive and even")
else:
print(num, "is positive and odd")
elif num < 0:
print(num, "is negative")
else:
print("Zero")
# Trace for input 7:
# num = 7
# 7 > 0 → True → enter first if
# 7 % 2 == 0 → 1 == 0 → False → enter else
# Output: "7 is positive and odd"Pitfalls & Common Errors
= vs == Confusion
= is assignment, == is comparison.
Writing if x = 5: is a SyntaxError. It must be if x == 5:.
Forgetting the Colon
if x > 5 → SyntaxError. Every if, elif, else
must end with :
Indentation Errors
Python uses indentation to define blocks. Inconsistent indentation causes
IndentationError. Use 4 spaces consistently.
Wrong Operator Precedence
not has higher precedence than and, which is higher than
or. Use parentheses when combining: (a or b) and c is different from
a or (b and c).
Pro-Tips for Exams
Exam Strategy
- When tracing a flowchart, follow ONLY one path at a time based on the input value
- For
and: if the first condition is False, the whole expression is False (short-circuit) - For
or: if the first condition is True, the whole expression is True (short-circuit) - Remember that
elifis checked only if ALL previous conditions were False - Draw a truth table if a complex boolean expression confuses you
Graded Tasks
Complete the truth table for: A and (B or not C) for all combinations of A, B,
C.
Explain the difference between if/if/if (three separate ifs) and
if/elif/else. Give an example where they produce different results.
Write a program that reads a year and determines if it is a leap year. (Divisible by 4, but not 100, unless also divisible by 400.)
Implement the following flowchart: Input a temperature → If temp > 30: "Hot" → Else if temp > 20: "Warm" → Else if temp > 10: "Cool" → Else: "Cold".
Given x = 15, trace and determine the output of:
if x > 10 and x < 20: print("A") elif x > 5 or x == 15: print("B") else: print("C")
Design a quiz program that asks 3 true/false questions, counts the score, and outputs a grade based on the number correct (3 = Excellent, 2 = Good, 1 = Fair, 0 = Fail).
Self-Check Quiz
True and False?True or False?not True?= and ==?
elif without if?