Programming Logic Essentials
Boolean Logic Basics
The Logic of True and False
At the heart of every complex decision a program makes lies a simple question: is something true or false? This binary choice is the foundation of all computation. In programming, we represent these values using the Boolean data type, named after the mathematician . A boolean can only hold one of two values: true or false.
Think of it as a light switch. It can't be halfway on; it's either on (true) or off (false). This simplicity is powerful. By combining these simple true/false values, we can build logic that guides everything from a user logging in successfully to a rocket ship adjusting its trajectory.
Boolean Logic centers around the fundamental concept that all values are either True or False and can be represented by either a 1 bit or a 0 bit.
Combining Truths with Operators
To make useful decisions, we need to combine different boolean values. We do this with logical operators. They take one or more boolean values as input and produce a single boolean value as output. The three most fundamental operators are AND, OR, and NOT.
The AND operator, often written as &&, checks if all conditions are true. The result is true only if every input is true. If even one input is false, the entire expression becomes false.
For example, to access a secure system, you might need a valid username AND a valid password. Both must be correct.
| A | B | A && B |
|---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
let hasValidKey = true;
let isDoorUnlocked = true;
let canEnter = hasValidKey && isDoorUnlocked; // Result: true
let hasSufficientFunds = true;
let itemIsInStock = false;
let canPurchase = hasSufficientFunds && itemIsInStock; // Result: false
Next is the OR operator, written as ||. It checks if at least one condition is true. The result is true if any of the inputs are true. It's only false when all inputs are false.
You could get a discount if you have a coupon OR it's a holiday sale. You only need one of those conditions to be met. Many programming languages use an optimization called . If the first part of an || expression is true, the language doesn't even bother to check the second part, because the outcome is already known.
| A | B | A || B |
|---|---|---|
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false |
let hasCoupon = false;
let isMember = true;
let getsDiscount = hasCoupon || isMember; // Result: true
let isWeekend = false;
let isHoliday = false;
let isDayOff = isWeekend || isHoliday; // Result: false
Finally, the NOT operator, !, is the simplest. It takes a single boolean value and inverts it. It flips true to false and false to true. This is useful for checking if a condition is not met.
We can use to map out the results of these operations for every possible combination of inputs. They provide a clear, visual reference for how each operator behaves.
| A | !A |
|---|---|
true | false |
false | true |
let isLoggedIn = true;
let isLoggedOut = !isLoggedIn; // Result: false
let engineOn = false;
let canDrive = !engineOn; // This is a bug! The result is true, but you can't drive.
The last code example highlights a key point: your logic is only as good as the conditions you set.
!engineOnistrue, but that doesn't mean the car is drivable. Logic must accurately model the real-world rules.
With these three simple operators, you have the complete toolkit to build any logical condition, no matter how complex. Let's review what we've covered.
The Boolean data type, which can only be true or false, is named after which mathematician?
In programming, what is the result of the expression !true?
Mastering AND, OR, and NOT is the first step toward writing intelligent, responsive code that can make decisions and react to changing conditions.