And, or, not
Combine several conditions into one answer. About 9 minutes.
Two questions at once
A fairground ride says: you must be tall enough and have a ticket. Both must be true to get on. That joining word is and.
Sometimes one is enough. A shop gives a discount to students or to people over 60. That joining word is or: the answer is true when at least one of the questions is true. Both being true also counts.
can_ride = tall and has_ticket discount = student or senior
Not, and brackets
The word not flips an answer: true becomes false, and false becomes true. Use it for rules like "divisible by 4, but not by 100".
When you mix and with or, use brackets to show what goes with what, just like in arithmetic. The leap year rule is a good example: a year is a leap year if it divides by 4 but not by 100, or if it divides by 400.
by4 = year % 4 == 0 by100 = year % 100 == 0 by400 = year % 400 == 0 leap = (by4 and not by100) or by400
- by4
- True
1900 divides evenly by 4, so by4 is true.
Is the year 2000 a leap year, by the rule: divisible by 4 and not by 100, or divisible by 400?
- AYes
- BNo, because it divides by 100
- CNo, because it isn't divisible by 4
Show the answer
Yes. 2000 divides by 100, so the first part fails. But it divides by 400, so the second part is true.
Leap year
A year is a leap year when it is divisible by 4 but not by 100, or when it is divisible by 400. Return true if the year is a leap year.
year = 2024 → true
1 ≤ year ≤ 9,999