Logical Operators
Combining conditions means joining tests together, or reversing a test, so that a decision can depend on more than one fact, or on the opposite of a fact. You do this with the logical operators AND, OR and NOT, which work alongside relational operators such as >= and ==.
A relational operator compares two values and produces True or False. AND and OR combine two of those results. AND is True only when both conditions are True; OR is True when at least one condition is True. NOT acts on one condition, or on one bracketed group, and reverses it, so True becomes False and False becomes True: NOT (day == "Saturday") is True on a Monday.
In a selection, such as an if statement, the condition decides which branch runs. In iteration, a condition decides whether a loop repeats: a while loop might continue while a player has lives left AND the timer has not run out. The combined condition is still a single True or False result, so it can control either kind of flow.
A climbing wall allows entry when someone is at least 140 cm tall and has a wristband, or when they are an instructor. That rule is (height >= 140 AND has_wristband) OR is_instructor. It lets in a short instructor. The different condition height >= 140 AND (has_wristband OR is_instructor) does not: it requires the height check to pass first. Brackets show exactly which parts are worked out and then combined, and which part a NOT reverses.
A community centre books its meeting room when there are no more than 8 people and an adult is present, or whenever a staff member asks.
people = int(input("How many people? "))
adult = input("Adult present? yes or no: ")
staff = input("Staff member? yes or no: ")
if (people <= 8 and adult == "yes") or staff == "yes":
print("Room booked")
else:
print("Booking refused")Output when the user types 6, yes, no:
How many people? 6
Adult present? yes or no: yes
Staff member? yes or no: no
Room booked
When you follow or write an algorithm, state each comparison fully, for example score >= 40 AND score < 70, and add brackets wherever the grouping changes the meaning.
Edexcel GCSE Computer Science · Logical Operators
Edexcel GCSE Computer Science · Selection (IF)
Edexcel GCSE Computer Science · Condition Controlled Iteration (While Loops)
Edexcel GCSE Computer Science · Input Validation
Edexcel GCSE Computer Science · Relational Operators