Operators for Boolean Logic#
Quite often, a single condition is not sufficient. You need to combine it with other conditions to meet the criteria you have. You want to go swimming? You will probably check your calendar if you have time for that. And you might check the weather. BOOM. Two conditions! When we combine various statements that can be true
✅ or false
❌, we make use of boolean logic.
While nesting of if statements is absolutely a thing, there should probably be an easier way, right? Right.
AND: When All the Things must be true
✅#
Like in the introduction, let's check for two things at once: my mouse pointer must be in a certain location on the X
axis but it must also be in a certain location on the Y
axis! We call this a logical and
, which is written down as the operator &&
. (This is also called a conjunction, but that's not something we're using in everyday conversations.)
void setup() {
size(400, 400);
}
void draw() {
if (mouseX > 200 && mouseY < 70) {
background(0, 255, 0);
} else {
background(255, 0, 0);
}
noFill();
rect(200, 0, 200, 70);
fill(0);
text("rectangle just for visual reference", 210, 68);
}
These can be written down in neat little tables like these
Statement A | Statement B | Result of Logical AND |
---|---|---|
false ❌ |
false ❌ |
false ❌ |
false ❌ |
true ✅ |
false ❌ |
true ✅ |
false ❌ |
false ❌ |
true ✅ |
true ✅ |
true ✅ |
Computers are Lazy
If you have a couple of statements that are combined with &&
, your computer will just stop at the first one that's false
❌. Because after that, nothing could change the result anyway.
OR: When any of the things must be true
✅#
Sometimes many different things can lead to the same result. I come too late to class if my bus breaks down. But I also come late to class if I overslept. If we're being honest, it's a wonder we're all in class thursday morning.
problemCounter = 0
if (oversleep) {
problemCounter++
}
if (bus broke down) {
problemCounter++
}
if (problemCounter > 0) {
arrive late
} else {
arrive on time
}
But that's mighty long. Let's try this again:
When we make statements like these, we can combine them with a logical OR
operator. This is written down as two pipe symbols ||
. (This is also called a disjunction, but this also doesn't come up naturally.) Let's have another example:
void setup() {
size(400, 400);
}
void draw() {
if (mousePressed || mouseY > 200) {
background(0, 255, 0);
} else {
background(255, 0, 0);
}
}
(you can see the "click" whenever the cursor is flashing yellow)
Here the background gets turned green if any of the conditions are met. Crucially, they could even both be true at the same time. The logical OR
is slightly different from "or" in our spoken languages that way.
Statement A | Statement B | Result of Logical OR |
---|---|---|
false ❌ |
false ❌ |
false ❌ |
false ❌ |
true ✅ |
true ✅ |
true ✅ |
false ❌ |
true ✅ |
true ✅ |
true ✅ |
true ✅ |
Computers are Lazy here, too!
If you have a couple of statements that are combined with ||
, your computer will just stop at the first one that's true
✅. If just any of these need to be true, that's good enough!
NOT: Doing the opposite#
The last boolean operation is NOT
, written in code as !
. (This is called negation, and this term actually does come up somewhat frequently).
Statement | Result of Logical NOT |
---|---|
false ❌ |
true ✅ |
true ✅ |
false ❌ |
Multiple ways to reach the same result#
Most of the time you will have many ways to reach the same result. Here are a few examples:
mouseX < 100
can also be written as the opposite !(mouseX >= 100)
These two examples are also the same, we just swapped the if and else blocks of code around.
It is a good idea to write code as readable as possible. Thinking about how you phrase your conditions can play a role in readability.
Combining more than two#
You can mix and match all of these, and sometimes you even need to put them in parentheses (the same rules as in math: stuff in parentheses gets evaluated first).
Here we combine four checks. Can you guess where this exact code would be useful?
Solution
This is what we call a "bounding box". We check if a point (for example the coordinates of our pointer) are within a rectangle. This is a pretty simple check and is done all the time. If you've ever clicked a button on a screen, chances are that a check like this was responsible for it. In our example, the button would be a rectangle between 100 and 400 pixels on the X
-axis and between 70 and 150 pixels on the Y
-axis. If you wanted to draw that button, you would run rect(100, 70, 300, 80)
.
This next one could be in your game. Let's say you have two game modes, one is about high score and the other is about speed-runs - if your player finishes their game you would want to check if they should add their name to the scoreboard.
if ((gameMode == 1 && score > HIGHSCORE) || (gameMode == 2 && time < FASTEST_TIME)) {
// let player enter their name
}
Relevant excerpt from Learning Processing#
(the section starts at 2:18:30 and runs through 2:28:47, the video should start and stop on these automatically.)