Propositional Logic:
Introduction to Propositional Logic

Discrete Math
 

If you've done any programming so far, you might be familiar with working with if statements and while loops. These both work with propositional logic to decide whether to branch the program, or continue looping a set of instructions.

Learning about propositional logic can really help with writing better programming logic, and also help you debugging your logic.


Propositions

A proposition is a statement that is true or false. Often, we think of these as "yes/no" questions.

Some examples of propositional statements are:

Fran has a cat.
The cat is black.
x > 10
x and y are equal


These are statements that can result in true (Fran DOES have a cat!) or false (Actually, the cat is orange). We could use these statements in a program, with an if statement...

string catColor;
cout << "What color is the cat? ";
cin >> catColor;

if ( catColor == "black" )
{
  // proposition was true...
}
else
{
  // proposition was false...
}

We can essentially think of it as a yes/no question of "Is the cat black?", where we could have different sets of operations to perform if the statement is true (the cat is black) or if the statement is false (the cat is not black).

On the flip side, we could not write a program with questions like "What is your favorite color?" -- That is not a yes/no question, and the computer wouldn't understand what that type of question is. Instead, if we really wanted to define behavior based on favorite color, we would need a series of if / else if statements asking about "is favorite color red?", "is favorite color blue?", and so on...

string favColor;
cout << "What is your favorite color? ";
cin >> favColor;

if ( favColor == "red" )
{
  // Code for red as favorite color...
}
else if ( favColor == "orange" )
{
  // Code for orange as favorite color...
}
else if ( favColor == "yellow" )
{
  // Code for yellow as favorite color...
}
// ... and so on ...
Self Review 1
Identify whether the following are propositions or not...

2 + 2 = 4


2 + 3 = 6


What movie do you want to see?


This class has 60 students


Compound Propositions

We can also create a compound proposition using the logical operators AND, OR, and NOT.

AND ∧

In C++, Java, and C#, the && symbol is used for a logical "AND". In discrete math, we often use .

If we have two propositional statements, e.g., p and q, then the compound proposition p ∧ q is only true when both sub-statements are also true. If even one of the sub-statements are false, then the entire compound statement is false.

Fran has a cat AND Fran has a dog.

The entire compound proposition is true only if both sub-statements are true. This proposition asserts that Fran has both a cat and a dog at the same time, not just one or the other.

Sub-statement 1Sub-statement 2Compound Proposition
Fran has a catFran has a dogFran has a cat
AND Fran has a dog.
In English...
True True True Fran has a cat. Fran has a dog.
True False False Fran has a cat. Fran does not have a dog.
False True False Fran does not have a cat. Fran has a dog.
False False False Fran does not have a cat. Fran does not have a dog.
OR ∨

In C++, Java, and C#, the || symbol is used for a logical "OR". In discrete math, we often use .

If we have two propositional statements, e.g., p and q, then the compound proposition p ∨ q is true when one or both sub-statements are also true. The result is only false when all sub-statements are false.

Fran has a cat OR Fran has a dog.

The entire compound proposition is true if at least one sub-statement is true. This statement asserts that Fran has a cat or a dog. She could have one or the other, or she could have both, and all these scenarios result in a true outcome.

Sub-statement 1Sub-statement 2Compound Proposition
Fran has a catFran has a dogFran has a cat
OR Fran has a dog.
In English...
True True True Fran has a cat. Fran has a dog.
True False True Fran has a cat. Fran does not have a dog.
False True True Fran does not have a cat. Fran has a dog.
False False False Fran does not have a cat. Fran does not have a dog.
NOT ¬

In C++, Java, and C#, the ! symbol is used for a logical "NOT". In discrete math, we often use ¬ (or sometimes ~).

NOT operates on one statement. Let's say we have the proposition p. If p is true, then the result of ¬p is false. If p is false, then the result of ¬p is true.

It is NOT true that... The program is done.

Usually NOT is used as a "a is not equal to b" scenario...

if ( a != b ) // ...
but it can also be used to negate logical statements, such as if we wanted to keep running the program while "done" is not true...
bool done = false;
while ( !done )   // While the program is NOT done...
{
  // ... then show main menu, process stuff, whatever ....
}

Sub-statement 1Compound Proposition
The program is doneIt is NOT true that... The program is doneIn English...
True False The program is done.
False True The program is not done.
Self Review 2
Identify whether the following compound statements result in TRUE or FALSE...

Printer is online = true,
Printer has paper = false,
Statement: Printer is online ∧ Printer has paper.


Printer is online = true,
Printer has paper = false,
Compound statement: Printer is online ∨ Printer has paper.


Printer is online = true,
Compound statement: It is NOT true that... The printer is online.


Printer is online = false,
Compound statement: It is NOT true that... The printer is online.


And now... A dramatic workplace play: The printer is offline - AND - The printer is out of paper.


Jim: Hey Janet, we can't print documents; the printer is offline AND the printer is out of paper.
Janet: Alright Jim. Thanks for the heads up.

( ... later ... )

Rahaf: Hey Janet, looks like the printer is offline, but it has paper.
Janet: Has paper?? Jim lied to me!!

( ... later ... )

Rose: Hey Janet, the printer is online but it's out of paper.
Janet: Well, which is it?!?!

( ... later ... )

Anuraj: I just printed out some docs... of course the printer is online and it has paper!
Janet: That's the complete opposite!!

Propositional variables

When working with propositional logic, we commonly shorten a propositional statement to a variable. For example, we can represent "she is in Discrete Math" with d, and "she is majoring in Computer Science" with c. We could then write out statements like:

  • d ∧ c
    She is in Discrete Math AND she is majoring in Computer Science.
  • d ∧ ¬c
    She is in Discrete Math AND she is NOT majoring in Computer Science.
  • ¬d ∧ c
    She is NOT in Discrete Math AND she is majoring in Computer Science.
  • ¬d ∧ ¬c
    She is NOT in Discrete Math AND she is NOT majoring in Computer Science.

Propositional variables should be one letter in length. (though when programming, you should use longer variable names :)

Self Review 3
Given the propositional variables p: I am a pirate, and g: I drink grog, and ∧ for AND, ∨ for OR, and ¬ for NOT, try translating the following into symbolic statements on paper. Click on "reveal" to see the solution.

I am a pirate AND I drink grog.


I am a pirate AND I don't drink grog.


I am a pirate OR I drink grog.


Either I am a pirate OR I don't drink grog.




(( Work in progres... ))



Written by Rachel Wil Sha Singh

Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.