Course 3.2Lesson 4 of 5
A truth table lists every possible combination of True and False for the parts of a condition and the result for each. With two parts there are four rows, and with three there are eight. A truth table proves that two conditions are the same. The rule of not says that not (A and B) is the same as (not A) or (not B), and not (A or B) is the same as (not A) and (not B). Use it to simplify a condition. A leap year check is a good example, with the rule: divisible by 4, except for a century year that is not divisible by 400.
Try the code and the simulations beside the lesson.
After this lesson you can
not (A and B) into a simpler form with the rule of not, andA combined condition can be hard to judge in the head, especially with not and brackets. A truth table is a way to be sure. It lists every possible combination of True and False for the parts, and the result for each.
With two parts, a and b, there are four combinations. With three parts, eight. Let the computer write the table.
Look at this sentence: "It is not true that I am tired and hungry". It means I am either not tired, or not hungry, or both. The word and has turned into or when the not went inside. This is called the rule of not (also known as De Morgan's law, after the mathematician who wrote it down).
not (A and B)is the same as(not A) or (not B)
not (A or B)is the same as(not A) and (not B)
Let the truth table prove it for the first rule.
Why is it useful? Sometimes a condition is written with a not in front of a bracket, and it is hard to read. The rule gives a simpler form. For example,
if not (age >= 18 and has_ticket): is the same as if age < 18 or not has_ticket:.
Notice that not (age >= 18) became age < 18. A not in front of a comparison can be absorbed into the comparison: not (a > b) is a <= b.
The rule for a leap year is: a year is a leap year if it is divisible by 4. But a century year (divisible by 100) is a leap year only if it is also divisible by 400. So 2024 is a leap year, 1900 is not, and 2000 is.
(year % 4 == 0 and year % 100 != 0) or year % 400 == 0
or: either way is enough.The brackets are important. A rule with an exception is typical: an and with a not for the exception, and an or that gives the exception to the exception.
The brackets matter: the and part is the usual rule, and or year % 400 == 0 is the exception for century years.
and, or and not, and put in brackets.and into or when moving a not inside a bracket.and part.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
not (A and B) equals (not A) or (not B). not (A or B) equals (not A) and (not B).not (a > b) is a <= b.and with not, and an or for the exception to the exception.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
not (x > 5 and y > 5).Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.