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.
Some examples of propositional statements are:
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 ...
We can also create a compound proposition using the logical operators AND, OR, and NOT.
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 1 | Sub-statement 2 | Compound Proposition | |
---|---|---|---|
Fran has a cat | Fran has a dog | Fran 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. |
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 1 | Sub-statement 2 | Compound Proposition | |
---|---|---|---|
Fran has a cat | Fran has a dog | Fran 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. |
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 1 | Compound Proposition | |
---|---|---|
The program is done | It is NOT true that... The program is done | In English... |
True | False | The program is done. |
False | True | The program is not done. |
( ... later ... )
( ... later ... )
( ... later ... )
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:
Propositional variables should be one letter in length. (though when programming, you should use longer variable names :)
(( Work in progres... ))
Written by Rachel Wil Sha Singh
This work is licensed under a Creative Commons Attribution 4.0 International License.