1
Social and ethical implications of Boolean logic & Conditional Statements in technology
Sana Sarin; Julius Henriques; and Rutwa Engineer
Overview
This module introduces you to the fundamentals of Boolean logic and conditional statements, showing how they drive decision-making in safety-critical systems, which are systems that may threaten people’s health and safety, the environment, or the economy if they were to fail. Specifically, you will learn about their importance in healthcare alert systems and autonomous technologies. You will create truth tables from Boolean operations, identify logical errors that could lead to system failures, and build reliable code through hands-on activities.
Learning Objectives
-
Apply Boolean variables and logical operators (AND, OR, NOT) to evaluate expressions
- Construct conditional statements (if, elif, else) to control program flow based on input conditions
- Identify and correct errors in conditional statements
- Investigate the impact of logical errors in Boolean expressions on safety-critical systems, such as healthcare alerts and autonomous technologies
Introduction
What are boolean variables?
Boolean variables are variables of the bool data type in Python that hold the values True or False.
x = False is_happy = True
When handling Booleans numerically, False evaluates to 0 and True evaluates to 1.
int(x) == 0 int(is_happy) == 1
When converting numbers to Boolean values, 0 evaluates to False, while any other numerical value evaluates to True.
bool(5) is True bool(-17.6) is True bool(0) is False
Boolean operations
Boolean values can be chained together using logical operators to form compound Boolean expressions.
and
Returns True if both operands are true and False when either operand is False.
(is_happy and is_happy) == True (x and is_happy) == False
or
Returns True if at least one operand is True and False when both operands are False.
(x or is_happy) == True (x or x) == False
not
Inverts the value of the operand.
not x == True
Activity 1: Building Truth Tables
For this activity, we are going to build a truth table to summarize these Boolean operations. The first row has already been completed for you:
A | B | A AND B | A OR B | NOT A |
False | False | False | False | True |
Using your knowldege of Boolean operations, fill in the rest of the rows:
A | B | A AND B | A OR B | NOT A |
False | True |
A | B | A AND B | A OR B | NOT A |
True | False |
A | B | A AND B | A OR B | NOT A |
True | True |
Conditional Statements
Some introduction about how boolean operations or logic is used to form conditions in conditional statements to control program flow depending on the values they evaluate to.
Conditions
Conditions are logical expressions that result in a Boolean value (True or False).
x > 5
Conditional statements
Logical expressions are used to make conditional statements which enable decision-making in programs, allowing for different code to execute based on different inputs or program state.
if
statement
An if
statement executes its body of code if its condition is True. Otherwise, the program moves to the next statement or line of code, and the body is skipped.
if is_cold and is_raining:
print("Wear a jacket and bring an umbrella.")
elif
statement
An elif
statement, short for else-if
, acts to chain if statements together and is only checked if the statement preceding it is False. The preceding statement may be an if
statement or another elif
statement.
elif is_cold:
print("Wear a jacket.")
elif is_raining:
print("Bring an umbrella.)
else
statement
An else
statement executes its body of code if all other preceding statements are False.
else:
print("You don't need a jacket or an umbrella.")
Activity 2: Building Decision-Making Programs
The idea of autonomous vehicles or self-driving cars has been an emerging trend in recent years that have seen great developments to its functionality, with some already being used on roads today. These vehicles have the potential to improve road safety, reduce fatalities, and increase the efficiency of transportation. This task is not easy, as these decision-making programs must dynamically consider multiple factors such as environmental conditions, the status of other drivers on the road, pedestrians, and traffic conditions. In this activity, we will explore how considering these conditions can influence different outcomes for autonomous vehicles by completing these functions:
.
Mistakes in Boolean Logic
There are cases where a set of conditions may be logically contradicting or defective. It is important to be wary of these cases as they may cause a program to malfunction or behave unpredictabily.
Tautology
The case where a statement always evalutes to True regardless of its input conditions.
x or not x
– this statement is True if either (x) or (not x) is True, but since at least one of them must be True, the statement is always True.
Contradiction
The case where a statement always evaluates to False regardless of its input conditions.
x and not x
– this statement is False if either (x) or (not x) is False, but since at least one of them must be False, the statement is always False.
Absorption
Absorption occurs in situation where multiple conditions are involved, where the statement only depends on the state of one condition and is unaffected by the state of the other condition.
x or (x and y)
– This statement is True if x or (x and y) is True. If x is True, then the statement evaluates to True and (x and y) is skipped. If x is False, the program checks (x and y). It will see that x is False and skip y, so (x and y) evaluates to False, and making the entire statement False. In each case, the statement only depends on the state of x.
Activity 3: Boolean Logic in Clinical Decision Support Sysetms
These errors in boolean logic conditions were able to find their way into various healthcare organizations, that would use these statements in healthcare alert systems to support clincal decision-making. A study was conducted on nine healthcare organizations that examined 260,698 logic statements, 209 of which had identified errors. This highlights the importance of handling boolean operations carefully as it needs to be something people can reliably put their trust in otherwise it could potentially misguide or harm many lives. Some of these types of errors were found in each organization in this study, that claimed they would use these alerts to influence their clinical decisions. In this activty, we will examine various cases that are similar to the ones in this study, where we will try to identify the types of logic errors present in these clincally-inspired programs. Once identified, try editing the code to fix the errors:
References
-
Rausand, M. (2014). Reliability of safety-critical systems : theory and application. Wiley. https://books-scholarsportal-info.myaccess.library.utoronto.ca/en/read?id=/ebooks/ebooks3/wiley/2014-04-08/2/9781118776353#page=15
- Wright, A., Aaron, S., McCoy, A. B., El-Kareh, R., Fort, D., Kassakian, S. Z., Longhurst, C. A., Malhotra, S., McEvoy, D. S., Monsen, C. B., Schreiber, R., Weitkamp, A. O., Willett, D. L., & Sittig, D. F. (2021). Algorithmic detection of boolean logic errors in clinical decision support statements. Applied Clinical Informatics, 12(01), 182–189. https://doi.org/10.1055/s-0041-1722918
- Schwarting, W., Alonso-Mora, J., & Rus, D. (2018). Planning and decision-making for autonomous vehicles. Annual Review of Control, Robotics, and Autonomous Systems, 1, 187–210. https://doi.org/10.1146/annurev-control-060117-105157
- Das, U., Lawson, A., Mayfield, C., & Norouzi, N. (2024, March 13). 4.1 Boolean values – Introduction to Python Programming | OpenStax. https://openstax.org/books/introduction-python-programming/pages/4-1-boolean-values
- Das, U., Lawson, A., Mayfield, C., & Norouzi, N. (2024b, March 13). 4.2 If-else statements – Introduction to Python Programming | OpenStax. https://openstax.org/books/introduction-python-programming/pages/4-2-if-else-statements
- Das, U., Lawson, A., Mayfield, C., & Norouzi, N. (2024c, March 13). 4.3 Boolean operations – Introduction to Python Programming | OpenStax. https://openstax.org/books/introduction-python-programming/pages/4-3-boolean-operations
- Das, U., Lawson, A., Mayfield, C., & Norouzi, N. (2024d, March 13). 4.5 Chained decisions – Introduction to Python Programming | OpenStax. https://openstax.org/books/introduction-python-programming/pages/4-5-chained-decisions