Combining questions
After this: Join two questions into one and say when the joined answer is yes.
On this page · 6 sections · 3 checks
A cinema shows a film for adults only. To get in, you must be 18 or older, and you must have a ticket. That is two questions. The person at the door lets you in only when the answer to both is yes.
So far each decision asked one question. This lesson joins two questions into one, in three ways: both must be yes, at least one must be yes, and the opposite of an answer.
Both: and
The word andThe sign && joins two conditions into one bigger condition. The joined condition is Truetrue only when both sides are Truetrue. One no is enough to make the whole answer no.
Here is every way the cinema check can turn out. The box has_tickethasTicket holds a yes/no value, so it is already a condition on its own.
age >= 18 | has_tickethasTicket | age >= 18 and has_ticketage >= 18 && hasTicket |
|---|---|---|
| yes | yes | yes |
| yes | no | no |
| no | yes | no |
| no | no | no |
Work one out with plain numbers: 5 > 3 is yes, 2 > 4 is no, so 5 > 3 and 2 > 45 > 3 && 2 > 4 is no. The joined answer is Falsefalse.
At least one: or
The word orThe sign || also joins two conditions, but it is satisfied by one yes. The joined answer is no only when both sides are no.
5 > 3 or 2 > 45 > 3 || 2 > 4 is yes, because the left side is yes. It does not matter that the right side is no. The joined answer is Truetrue.
The opposite: not
The word notThe sign ! flips one answer: yes becomes no, and no becomes yes. not (5 > 3)!(5 > 3) is no, because 5 > 3 is yes. The brackets say which answer to flip.
The trace below stores the three answers in three boxes, then prints them. Step through and watch the boxes appear.
both = 5 > 3 and 2 > 4either = 5 > 3 or 2 > 4flipped = not (5 > 3)print(both)print(either)print(flipped)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
Using it in a decision
A joined condition goes wherever a plain one goes. Here is the cinema check as a program. The age is 20, so the age side is yes, but there is no ticket.
- Run the programRun it. The age question is yes and the ticket question is no. Watch what the joined condition does.
age = 20has_ticket = Falseif age >= 18 and has_ticket: print("Come in")else: print("No entry")run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
The joined condition is no, so the program jumps to the else part.
Change Falsefalse to Truetrue and run again; then change the age to 16 as well.
Check yourself
When is age >= 18 and has_ticketage >= 18 && hasTicket Truetrue?
What does this print?
age = 16has_ticket = Trueif age >= 18 or has_ticket: print("A")if age >= 18 and has_ticket: print("B")print("C")int age = 16;boolean hasTicket = true;if (age >= 18 || hasTicket) { System.out.println("A");}if (age >= 18 && hasTicket) { System.out.println("B");}System.out.println("C");What is the value of not (2 > 4)!(2 > 4)?
Honesty notes
- Join two questions so that the answer is yes only when both are (and in Python, && in Java).
- Join two questions so that the answer is yes when at least one is (or in Python, || in Java).
- Flip an answer (not in Python, ! in Java).
- Read a four-row table that shows every way two yes/no answers can combine.