Fall 2024 - P3
3.5 Booleans; All About Booleans (Period 3)
Student led teaching on Booleans. Learn how booleans are used in decision-making with logical operators.
All About Booleans
What Are Booleans?
In programming, a Boolean is a data type that can hold one of two values: true or false. These values are used to represent truth values, which are fundamental in controlling the flow of a program.
Basic Boolean Values
True: Indicates a logically true condition. False: Indicates a logically false condition.
Boolean Expressions
A Boolean expression is a statement that evaluates either true or false. Common Boolean expressions often involve comparison or relational operators, such as:
Equal to (==)
Not equal to (!=)
Greater than (>)
Less than (<)
Greater than or equal to (>=)
Less than or equal to (<=)
%%javascript
// Example 1: Checking if a boolean is true
const variableA = true;
const variableB = false;
if (variableA) {
console.log("VariableA is true!");
} else {
console.log("VariableA is false!");
}
if (variableA && !variableB) {
console.log("VariableA is true and VariableB is false");
}
if (variableA || variableB) {
console.log("Either VariableA or VariableB is true");
}
<IPython.core.display.Javascript object>
%%javascript
// booleans in javascript
const number = 1;
if (number > 10) {
console.log("Number is greater than ten!");
} else {
console.log("Number is not greater than ten!");
}
<IPython.core.display.Javascript object>
%%javascript
//Example of using Booleans with numbers in Python
const temperature = 105
is_hot = temperature > 100 //Boolean expression (WILL BE TRUE)
const number = 50
if (number > 100) {
console.log("It's a a big number day!")
} else {
console.log("It's a small number")
}
<IPython.core.display.Javascript object>