Mathematics for Computer Science

(Frankie) #1
3.2. Propositional Logic in Computer Programs 39

3.2 Propositional Logic in Computer Programs


Propositions and logical connectives arise all the time in computer programs. For
example, consider the following snippet, which could be either C, C++, or Java:

if ( x > 0 || (x <= 0 && y > 100) )
::
:
(further instructions)

Java uses The symbol||for “OR,” and the symbol&&for “AND.” Thefurther
instructionsare carried out only if the proposition following the wordifis true.
On closer inspection, this big expression is built from two simpler propositions.
LetAbe the proposition thatx > 0, and letBbe the proposition thaty > 100.
Then we can rewrite the condition as

AOR.NOT.A/ANDB/: (3.1)

3.2.1 Truth Table Calculation
A truth table calculation reveals that the more complicated expression 3.1 always
has the same truth value as
AORB: (3.2)
Namely, we begin with a table with just the truth values ofAandB:

A B A OR .NOT.A/ AND B/ AORB
T T
T F
F T
F F

These values are enough to fill in two more columns:

A B A OR .NOT.A/ AND B/ AORB
T T F T
T F F T
F T T T
F F T F
Free download pdf