Boolean Logic - Programming Basics
Categories: Foundation Lessons Programming ConceptsLearn about true/false values in programming with an interactive calculator
- � Boolean Logic: True or False?
� Boolean Logic: True or False?
What You’ll Learn
By the end of this lesson, you will:
- Know what boolean values are (true and false)
- Use comparison operators like
==and!= - Understand how computers make decisions
- Practice with an interactive calculator
What Are Booleans?
Booleans are simple: they can only be true or false. Think of them like light switches - they’re either ON or OFF.
Examples:
true= yes, on, correctfalse= no, off, incorrect
Computers use booleans to make decisions, just like you do every day!
Basic Boolean Operations
Comparing Numbers
console.log(5 == 5); // true - equal
console.log(5 != 3); // true - not equal
console.log(7 > 3); // true - greater than
console.log(2 < 8); // true - less than
Combining Conditions
// AND (&&) - BOTH must be true
console.log(true && true); // true
console.log(true && false); // false
// OR (||) - AT LEAST ONE must be true
console.log(true || false); // true
console.log(false || false); // false
// NOT (!) - FLIPS the value
console.log(!true); // false
console.log(!false); // true
Try It Out: Interactive Calculator
See how booleans work in a real calculator:
What’s Happening Behind the Scenes?
Calculator Status
Last Action: None
Calculator Checks:
- Ready for new number: true
- Has decimal point: false
- Number stored in memory: false
- Operation waiting: false
How the Calculator Uses Booleans
The calculator makes decisions using true/false questions:
// When you click a number button:
if (readyForNewNumber == true) {
// Show the new number
display = newNumber;
} else {
// Add to existing number
display = display + newNumber;
}
// When you click an operation (+, -, *, /):
if (hasStoredNumber == false) {
// Store the first number
storedNumber = currentNumber;
} else {
// Calculate with stored number
result = calculate(storedNumber, currentNumber);
}
Practice Exercises
Exercise 1: Predict the Result
Try to guess what these will show before testing:
5 == 5→ ?3 > 7→ ?true && false→ ?true || false→ ?!true→ ?
Exercise 2: Real-World Examples
Think about these everyday decisions as booleans:
- “If it’s raining AND I don’t have an umbrella, then I’ll get wet”
- “If I have homework OR there’s a test tomorrow, then I need to study”
- “If it’s NOT a school day, then I can sleep in”
Boolean Truth Tables
Quick reference for how booleans combine:
AND (&&) - Both must be true
| A | B | Result | |—|—|——–| | true | true | true | | true | false | false | | false | true | false | | false | false | false |
OR (||) - At least one must be true
| A | B | Result | |—|—|———-| | true | true | true | | true | false | true | | false | true | true | | false | false | false |
Why Booleans Matter
Booleans are everywhere in programming! They help computers:
- Make decisions (if this, then that)
- Check conditions (is the password correct?)
- Control what happens next in a program
Quick Quiz
- What are the only two boolean values?
- What does
5 > 3equal? - What does
true && falseequal? - Give an example of a boolean question from everyday life.
Answers: 1) true and false, 2) true, 3) false, 4) “Is it raining?” or “Am I hungry?”
Great job! You now understand boolean logic - the foundation of how computers make decisions!