Back

Activity 2.1 Boolean Logic

divider

Introduction

Activity 2.1

Boolean Logic

Topics

  • Application Logic
  • The Boolean Data Type
  • Boolean Logic Operators

Application Logic

Computer processors can perform more than just arithmetic calculations. They can also make decisions based on certain conditions.

This ability to make decisions allows programs to handle different outcomes.

Application Logic Samples

What possible outcomes may occur when I type in my age?

Boolean Age Screenshot

Application Logic Samples

What possible outcomes may occur when I try to login?

Boolean Login Screenshot

Application Logic Samples

How does the game know when to finish the round or match?

Boolean Game Screenshot

The Boolean Data Type

the boolean data type is used to store values that can be either true or false. It is used for logical expressions, which are questions or conditions that the computer can evaluate.

let isRaining = true;
let hasUmbrella = false;

Boolean Logic

The study of how logical expressions are formed and evaluated is called boolean logic.

In programming, these logical expressions are constructed using operators and values, and they always result in either true or false.

Boolean Logic Operators

Comparison Operators
Operator Name Description
== Equality Returns true if operands are equal.
!= Inequality Returns true if operands are not equal.
> Greater Than Returns true if left operand is greater than right.

Boolean Logic Operators

Comparison Operators
Operator Name Description
< Less Than Returns true if left operand is less than right.
>= Greater Than or Equal Returns true if left operand is greater than or equal to right.
<= Less Than or Equal Returns true if left operand is less than or equal to right.

Boolean Logic Operators

Logical Operators
Operator Name Description
&& Logical AND Returns true if both operands are true.
|| Logical OR Returns true if at least one operand is true.
! Logical NOT Returns true if the operand is false, and vice versa.

Sample Boolean Expressions

'abc' == 'abc'

'ABC' == 'abc'

100 != 99

'ABC' != 'abc'

100 > 100

100 >= 100

Sample Boolean Expressions

!true

!true == false

!(100 > 100)

Boolean Variables

Boolean values and expressions can be saved to a variable.

let health = 100;
let isAlive = health > 0;

Key Terms

Boolean
A data type that can only have one of two possible values: true or false. It is fundamental to computer logic, as it represents the outcome of a logical question or condition.
Logical Expression
A statement that a computer can evaluate to determine if it is true or false.

'F' → Fullscreen

Objectives

  • icon Evaluating boolean expressions
  • icon Saving boolean expression results to variables
divider

Activity Tasks

  • icon Create a new project named 2-1-boolean-logic.js.
  • icon Complete each task individually.

Task 1: Boolean Variables and Name Checks

  • icon Evaluate boolean expressions and save the results to variables.
2-1-boolean-logic.js
// Initialize variables
let name = "Mr. Mortimer";
let age = 30;
let balance = 150.00;
let isTeacher = true;
let isMillionaire = false;
// Boolean expressions
let nameIsMortimer = name == "Mr. Mortimer";
let canDrive = age >= 16;
let canBuyCar = balance > 31000;
let isSenior = age >= 65;
//

Task 2: Name Checks

  • icon Output the results of boolean expressions.
2-1-boolean-logic.js
// Initialize variables
let name = "Mr. Mortimer";
let age = 30;
let balance = 150.00;
let isTeacher = true;
let isMillionaire = false;
// Boolean expressions
let nameIsMortimer = name == "Mr. Mortimer";
let canDrive = age >= 16;
let canBuyCar = balance > 31000;
let isSenior = age >= 65;
console.log("-Name Checks-");
console.log(`Is my name Mr. Mortimer? ${nameIsMortimer}`);
console.log(`Is my name Mr. Mertens? ${name == "Mr. Mertens"}`);
console.log(`My name is not Mr. Merriman: ${name != "Mr. Merriman"}`);
alert("Press enter to continue...")
//

Task 3: Age-Related Checks

2-1-boolean-logic.js
// Initialize variables
let name = "Mr. Mortimer";
let age = 30;
let balance = 150.00;
let isTeacher = true;
let isMillionaire = false;
// Boolean expressions
let nameIsMortimer = name == "Mr. Mortimer";
let canDrive = age >= 16;
let canBuyCar = balance > 31000;
let isSenior = age >= 65;
console.log("-Name Checks-");
console.log(`Is my name Mr. Mortimer? ${nameIsMortimer}`);
console.log(`Is my name Mr. Mertens? ${name == "Mr. Mertens"}`);
console.log(`My name is not Mr. Merriman: ${name != "Mr. Merriman"}`);
alert("Press enter to continue...")
console.log("\n-Age-Related Checks-");
console.log(`Am I old enough to drive? ${canDrive}`);
let canRentCar = age >= 25;
console.log(`Am I old enough to rent a car? ${canRentCar}`);
console.log(`Am I eligible for a senior citizen discount? ${isSenior}`);
alert("Press enter to continue...")
//

Task 4: Monetary Checks

2-1-boolean-logic.js
// Initialize variables
let name = "Mr. Mortimer";
let age = 30;
let balance = 150.00;
let isTeacher = true;
let isMillionaire = false;
// Boolean expressions
let nameIsMortimer = name == "Mr. Mortimer";
let canDrive = age >= 16;
let canBuyCar = balance > 31000;
let isSenior = age >= 65;
console.log("-Name Checks-");
console.log(`Is my name Mr. Mortimer? ${nameIsMortimer}`);
console.log(`Is my name Mr. Mertens? ${name == "Mr. Mertens"}`);
console.log(`My name is not Mr. Merriman: ${name != "Mr. Merriman"}`);
alert("Press enter to continue...")
console.log("\n-Age-Related Checks-");
console.log(`Am I old enough to drive? ${canDrive}`);
let canRentCar = age >= 25;
console.log(`Am I old enough to rent a car? ${canRentCar}`);
console.log(`Am I eligible for a senior citizen discount? ${isSenior}`);
alert("Press enter to continue...")
console.log("\n-Bank Account Checks-");
console.log(`Do I have enough to buy a Dodge Challenger? ${canBuyCar}`);
let hasFunds = balance > 0;
console.log(`Do I have a positive bank balance? ${hasFunds}`);
alert("Press enter to continue...")
//

Task 5: Logical Not Operations

2-1-boolean-logic.js
// Initialize variables
let name = "Mr. Mortimer";
let age = 30;
let balance = 150.00;
let isTeacher = true;
let isMillionaire = false;
// Boolean expressions
let nameIsMortimer = name == "Mr. Mortimer";
let canDrive = age >= 16;
let canBuyCar = balance > 31000;
let isSenior = age >= 65;
console.log("-Name Checks-");
console.log(`Is my name Mr. Mortimer? ${nameIsMortimer}`);
console.log(`Is my name Mr. Mertens? ${name == "Mr. Mertens"}`);
console.log(`My name is not Mr. Merriman: ${name != "Mr. Merriman"}`);
alert("Press enter to continue...")
console.log("\n-Age-Related Checks-");
console.log(`Am I old enough to drive? ${canDrive}`);
let canRentCar = age >= 25;
console.log(`Am I old enough to rent a car? ${canRentCar}`);
console.log(`Am I eligible for a senior citizen discount? ${isSenior}`);
alert("Press enter to continue...")
console.log("\n-Bank Account Checks-");
console.log(`Do I have enough to buy a Dodge Challenger? ${canBuyCar}`);
let hasFunds = balance > 0;
console.log(`Do I have a positive bank balance? ${hasFunds}`);
alert("Press enter to continue...")
console.log("\n-Boolean Checks-");
console.log(`${name} is a teacher: ${isTeacher}`);
console.log(`${name} is a millionaire: ${isMillionaire}`);
let notMillionaire = !isMillionaire;
console.log(`So, ${name} isn't a millionaire? ${notMillionaire}`);
alert("Press enter to continue...")
//

Task 6: Logical AND & OR

2-1-boolean-logic.js
// Initialize variables
let name = "Mr. Mortimer";
let age = 30;
let balance = 150.00;
let isTeacher = true;
let isMillionaire = false;
// Boolean expressions
let nameIsMortimer = name == "Mr. Mortimer";
let canDrive = age >= 16;
let canBuyCar = balance > 31000;
let isSenior = age >= 65;
console.log("-Name Checks-");
console.log(`Is my name Mr. Mortimer? ${nameIsMortimer}`);
console.log(`Is my name Mr. Mertens? ${name == "Mr. Mertens"}`);
console.log(`My name is not Mr. Merriman: ${name != "Mr. Merriman"}`);
alert("Press enter to continue...")
console.log("\n-Age-Related Checks-");
console.log(`Am I old enough to drive? ${canDrive}`);
let canRentCar = age >= 25;
console.log(`Am I old enough to rent a car? ${canRentCar}`);
console.log(`Am I eligible for a senior citizen discount? ${isSenior}`);
alert("Press enter to continue...")
console.log("\n-Bank Account Checks-");
console.log(`Do I have enough to buy a Dodge Challenger? ${canBuyCar}`);
let hasFunds = balance > 0;
console.log(`Do I have a positive bank balance? ${hasFunds}`);
alert("Press enter to continue...")
console.log("\n-Boolean Checks-");
console.log(`${name} is a teacher: ${isTeacher}`);
console.log(`${name} is a millionaire: ${isMillionaire}`);
let notMillionaire = !isMillionaire;
console.log(`So, ${name} isn't a millionaire? ${notMillionaire}`);
alert("Press enter to continue...")
console.log("\n-Logical Operator Examples-");
let canDriveAndBuy = canDrive && canBuyCar;
console.log(`Am I old enough to drive AND have enough to buy a Dodge Challenger? ${canDriveAndBuy}`);
let canDriveOrBuy = canDrive || canBuyCar;
console.log(`Am I old enough to drive OR have enough to buy a Dodge Challenger? ${canDriveOrBuy}`);
//
divider

Sample Output

Your program output should something similar to the sample output below.

Sample Output
-Name Checks-
Is my name Mr. Mortimer? true
Is my name Mr. Mertens? false
My name is not Mr. Merriman: true
Press enter to continue... [Enter]
-Age-Related Checks-
Am I old enough to drive? true
Am I old enough to rent a car? true
Am I eligible for a senior citizen discount? false
Press enter to continue... [Enter]
-Bank Account Checks-
Do I have enough to buy a Dodge Challenger? false
Do I have a positive bank balance? true
Press enter to continue... [Enter]
-Boolean Checks-
Mr. Mortimer is a teacher: true
Mr. Mortimer is a millionaire: false
So, Mr. Mortimer isn't a millionaire? true
Press enter to continue... [Enter]
-Logical Operator Examples-
Am I old enough to drive AND have enough to buy a Dodge Challenger? false
Am I old enough to drive OR have enough to buy a Dodge Challenger? true
divider

Reflection Questions

You may write your reflection answers as comments at the bottom of your code.

  1. How do boolean expressions and logical operators connect to real-world decision-making processes, like the examples of age verification or logging in? Think about how they help a program "decide" what to do.
  2. The activity uses different types of operators (comparison and logical). Describe a scenario where you would need to use a combination of comparison operators and a logical operator (&& or ||) to get the correct result. For example, checking if a person is both old enough to drive AND has enough money to buy a car.
divider

Submission

Submit your activity and reflection answers to the appropriate dropbox.

Activity Complete