speed > 2000 | (speed <= 2000) | !(speed <= 2000) |
---|---|---|
F | T | F |
T | F | T |
The original program fragment used this boolean expression
!(speed > 2000 && memory > 512)
which is explained in the following truth table (repeated from a previous page):
speed > 2000 | memory > 512 | speed > 2000 && memory > 512 | !(speed > 2000 && memory > 512) |
---|---|---|---|
F | F | F | T |
F | T | F | T |
T | F | F | T |
T | T | T | F |
An equivalent program fragment used this boolean expression
speed <= 2000 || memory <= 512
which is explained in the following truth table:
speed > 2000 | memory > 512 | speed <= 2000 | memory <= 512 | speed <= 2000 || memory <= 512 |
---|---|---|---|---|
F | F | T | T | T |
F | T | T | F | T |
T | F | F | T | T |
T | T | F | F | F |
Each table has the same first two columns. The true/false values in the last column of each table are the same, which shows that the two boolean expressions are equivalent. One expression can be used in place of the other in a program.
Is there only one correct way to write an if
statement in a program?