We saw in the previous videos that while computers cannot think, they are very good at calculating things quickly. Computers understand decisions on a much lower level than humans. That said, if you want your computer to calculate a budget, you can tell it what to do, and it'll get it done in the blink of an eye! It does this by working with states and performing logical operations to arrive at the finished result.
States
There are a few key states you need to know to get your computer to make a decision. States are the answers to any questions you might ask the computer. Let's check them out!
Booleans
A boolean only has two values: true or false. Never anything else! It's what allows the computer to respond to simple statements. For example, you can give the computer a statement like "It's winter." The computer will evaluate that statement, then give a verdict. If it is currently winter, the computer will respond "true." If it's spring, summer, or autumn, the computer will respond "false."
Let's check out another example. Suppose you work at a store where you give a 5% discount for items purchased in June. To decide what discount to apply (if any), you’d have to check the following statement: "It’s June." This could either be true or false. If it's true, you can give the customer a discount. If it's false, you won't give them a discount. Who knew life could be so simple?
You can put these together in what’s called a boolean truth table:
POSSIBLE MONTH | STATEMENT: "IT'S JUNE" | DISCOUNT |
July | false(0) | 0% |
June | true(1) | 5% |
Now you know exactly when to give a discount!
Where do booleans come from?
Computers can only understand 0's and 1's. Can you tell what the computer is thinking below?
000010111110101000111101010101000101
That looks like a lot of nonsense, but computers can make decisions with zeros and ones. That's actually how booleans work and why they're so fundamental. With booleans, true is actually a one for the computer and false is a zero. In fact, it's an electrical on/off state inside of the computer. Let's play a little game. You decide to ask your computer some questions, and it's nice enough to give you some answers:
Q: Do you like food?
A: 1 (One means true to the computer, which is its way of saying "yes!")
Q: Do you get enough sleep?
A: 0 (Zero means false to a computer, which is its way of saying "of course not!")
Basically, the computer understands only zeros and ones. To provide it with instructions on how to give special customer discounts in June, you need to use the magic of true and false. ✨
Empty
Imagine you've just gotten a puppy. 🐶 You know you’re going to name it, but you haven’t decided on its name yet. If this puppy were being modeled as a computer object, the puppy's name would be empty. You've planned a spot for it, but it's currently empty - until you chose a name for it.
Null
Imagine you see a stray dog crossing the road. 🐕 Not only does it not have a name, you’re not planning on ever naming it. It’s not your dog, after all.
Empty vs null
Still confused about the difference? Try this image:
On the left, the toilet paper roll exists but doesn't have any more paper. It's empty. On the right, the roll doesn't exist at all! It's null.
Logical operators
Do you remember learning English and needing to understand the parts of speech? Let's discuss conjunctions. Without them, you’d be forced to express every complex idea in a series of short, simplistic sentences: It is Saturday. It is June. The temperature is 90 F.
Conjunctions allow you to form complex, elegant sentences and avoid the choppiness of multiple short sentences. Coordinating conjunctions allow you to join words, phrases, and clauses of equal grammatical rank in a sentence. Like you saw in the video, the most common coordinating conjunctions in software are AND, OR, and NOT.
Let’s check out each one of these in a little more detail.
NOT
NOT is very helpful when you’re working on security for a website. Imagine that you have your own blog. You leave the access open for people to read and comment, but you’re the only one who can write actual posts.
This is the perfect situation for NOT. Anyone who is NOT you (the author of the blog) cannot write on the website. Whenever a person requests to write something, the system can check their user status with the statement: "It’s NOT the author."
If the result is not false, that means that it’s true. It’s someone other than the author. The computer can reject the user’s request to write on your glorious page of thoughts.
If the result is not true, that means that it’s false. In other words, the user is you, the author! So the computer will grant your request to write on the blog.
AND
Suppose you are trying to plan your weekend. It's likely that the weather conditions will contribute to the decision. For example, if you are thinking about a trip to the beach, then asking if the temperature is above 90 F will produce a true/false answer.
However, you are more interested in the temperature on Saturday. You could map this out with the statements:
Is the temperature > 90 F? AND Is it Saturday?
The computer actually sees three logical operations in this context:
Is the temperature above 90 F?
Is the day Saturday?
Is the overall answer true or false?
Is it possible that one of the two questions is true while the other is false? The temperature might be 98 F, but the day of the week could be Tuesday! Also, it might be Saturday, but the temperature is a cool 72 F. Can you think of a case where neither logical operation would be true? How about a Monday with a temperature of 60 F?
Let’s get more practice with this idea by going back to the store example. Initially, you gave a 5% discount for items purchased in June, but what if you wanted to apply a special 8% discount for Saturdays in June?
Now you have to check two statements: It’s June. AND It’s Saturday.
An easy way to keep track of when to apply the discount, is to use a truth table, which lists all the possible combinations you could encounter. Let’s check out the one below:
Boolean truth table
Possibility | isJune | isSaturday | Discount |
July/Monday | false(0) | false(0) | 0% |
June/Wednesday | true(1) | false(0) | 5% |
October/Saturday | false(0) | true(1) | 0% |
June/Saturday | true(1) | true(1) | 8% |
As you can see, there are a few options that fulfill one of your requirements, but these aren’t taken into account. You need both requirements to be true in order get a discount.
OR
Now suppose you are trying to decide what shoes to wear today. Your shoes are in a big pile, and you are already running late! 👞👟👠
You want to wear sandals or any pair of shoes that will be comfortable. That means you have two statements: The shoes are sandals. OR The shoes are comfortable.
This time you are using OR rather than AND. Therefore, you are interested in either logical operation being true. The overall answer is true if either the shoes are sandals or they are comfortable.
Try it out for yourself!
Computers can only make logical decisions. Identify which of the following questions you believe a computer could answer:
What time is it in Paris?
How did you feel about your first day on the job or at school?
What do you think are the coolest TV shows this year?
Which employee is the highest paid?
What is the temperature where you live?
Of the questions you believe could be answered by a computer, sketch out the question as you might imagine it being presented to a computer (yes/no statements). Scroll back to Logical operations to see a few examples.
Summary
Computers use logical operations to solve problems.
Boolean values can be either true or false.
Other values are null or empty.
Key logical operators are NOT, AND, OR.