Chapter 12 - AND, OR, and NOT Operators

Remember our example from earlier with Charlie and the crying and the food and the love? Well if you don't, it looked like this:

if (charlie.isCrying) {
  if (charlie.isHungry) {
    charlie.provide('food');
  } else if (charlie.isThirsty) {
    charlie.provide('water');
  } else {
    charlie.provide('love');
  }
}

Another way for us to write the inner conditional (although less efficient, but it doesn't matter for now since "premature optimization is the root of all evil"):

// Assuming he's already crying:
if ( !(charlie.isHungry || charlie.isThirsty) ) {
  charlie.provide('love');
} else if (charlie.isHungry) {
  charlie.provide('food');
} else {
  charlie.provide('water');
}

The above code is probably more confusing than the conditional we had before it, but we should still try to understand it since it introduces a few concepts we will need in the future. So have a look at the first line:

if ( !(charlie.isHungry || charlie.isThirsty) ) {
  // magical code
}

Now I'm going to tell you that in most computer languages this: || (double pipe, which is located right under your backspace/delete key) means OR. So let's just look at what's inside the inner set of parentheses:

charlie.isHungry || charlie.isThirsty

This means what you think it means and is: Charlie is hungry OR thirsty. It's not a lot: Is the dog hungry or thirsty. It's as simple as it can be.

You do this all the time throughout the day, but it's important we take the time to understand this since the OR operator in computer-land is a non-exclusive OR which means if both values are truthy the result of the entire boolean expression will still be true. Just keep reading.

In your human life, when someone asks you this:

"Hey, Peleg, is the color of Charlie's fur black or gold?" You should answer "No" to that question because the color of Charlie's fur is black AND gold. He was lucky enough to have both colors, so in human-land an or in this case would result in false. Better example, someone asks you: "What is the color of this dress? 'Blue and black' or 'white and gold'?" A valid response from you should be blue and black OR white and gold, but not both!

Well computers' OR operators (||) are inclusive which means if both values are truthy the result of the whole expression will still be true:

(true || false)  // will return true
(false || true)  // true
(false || false) // false
(true || true)   // will STILL return true

Pay close attention to the last line above. In human language (true || true) would yield false!

So in our example with Charlie:

(charlie.isHungry || charlie.isThirsty)

If he is hungry, or if he is thirsty, or if both, the value of the boolean expression enclosed in the parentheses will be true!

But we're not stopping here. We enclosed our boolean expressions in a set of parentheses for a reason. We're going to switch the value of the entire expression. So whatever we get from the expression enclosed in parentheses (true or false), we negate it by using the negation operator, NOT, which is just an exclamation mark (!), also referred to as a bang. This special little guy means NOT and it just negates the value adjacent to it. So if we had a truthy value in our parentheses it'll now be false and vice versa.

!(charlie.isHungry || charlie.isThirsty)

So now you can say it like this: if Charlie is NOT hungry nor thirsty. Make sure you understand why we used the parentheses. If we neglected to add them, our negation would only operate on the first value (charlie.isHungry) and not the resulting value of both. And this leads us to the AND operator which is like an OR, but it's not -- it's an AND.

if (charlie.isHungry && charlie.isThirsty) {
  // some work
}

Meaning the interpreter will execute the code following that statement only if both conditions are truthy.

true && true   // will return true
true && false  // false
false && true  // false
false && false // false

This AND operator (&&) works just like it does in human languages. "Is Charlie's fur black and gold?" Yes. "Is it black and blue?" No. "Is it white and gold?" No. So obviously, in our most recent example, only if Charlie is hungry and thirsty will the code in the block of the conditional get executed.

Look at you, you're doing so well. If you still haven't set this book on fire, I'm proud of ya! This evening ditch the PBR and treat yourself to an adult beverage that doesn't come in a can.

Table of Contents
Home