Structure: Boolean Operators Flashcards

1
Q

True only if both conditions are met?

True if one condition is met?

True if operand is false?

A

&& (logical and)

if (digitalRead(2) == HIGH && digitalRead(3) == HIGH) // read two switches

{

// …

}

is true only if both inputs are high.

|| (logical or)

if (x \> 0 || y \> 0)
{
 // ...
}

is true if either x or y is greater than 0.

! (not)

if (!x)
{
 // ...
}

is true if x is false (i.e. if x equals 0).

Warning

Make sure you don’t mistake the boolean AND operator, && (double ampersand) for the bitwise AND operator & (single ampersand). They are entirely different beasts.

Similarly, do not confuse the boolean || (double pipe) operator with the bitwise OR operator | (single pipe).

The bitwise not ~ (tilde) looks much different than the boolean not ! (exclamation point or “bang” as the programmers say) but you still have to be sure which one you want where.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly