coding help
Two Boolean logic examples. One asks who I should date, with a girl XOR a boy. The other is about cats who hate each other, specifying an orange cat NAND a gray cat.

Examples of Boolean operators

Boolean logic is used in many programming languages. On wikis, that includes template parser functions and JavaScript. Many other languages use boolean logic too.

Boolean values are "true" or "false."[1] In programming, you can use boolean expressions for decision-making.[1]

Operators

Boolean operators are the backbone of Boolean logic. Common ones include AND, OR, and NOT. There are also a few more.

AND

AND means "both" or "all of the above." The condition is only true when every option is fulfilled. Example:

AND: It isn't a true girls' night unless all the girls are here. We need Cecilia AND Annie AND Katherine AND me. If we don't have everyone, it's not a true girls' night.

When you write code, an AND might be represented by a & or && symbol.[2] For example, a girls' night needs "Cecilia & Annie & Katherine & me."

OR

OR means "at least one of these." The result is true if at least one option is true.

OR: I don't want to go to the movies alone. I can go with Grace OR Shafi OR Alan OR Tim. If at least one friend says yes, I'll have fun.

In code, an OR might be represented by a | or || symbol.[2] For example, a non-solo movie involves "Grace | Shafi | Alan | Tim."

NOT

NOT is pretty self-explanatory. It produces "true" if something is false and "false" if something is true. Example:

NOT: I can eat anything from the restaurant if it is NOT spicy. So I can eat not-spicy foods and I can't eat spicy foods.

In code, a NOT is sometimes represented by an exclamation point before a value.[2] For example, my food should be !spicy.

XOR

XOR stands for "exclusive or."[3] That means "one of these, but not neither and not both."

XOR: I matched with Ada and Charles on a dating app. If I want a dinner date tonight, I can go with Ada or Charles, but I can't bring neither or both. I can have a dinner date tonight with Ada XOR Charles.

In code, an XOR is often represented by ^.[2] For example, a dinner date involves "Ada ^ Charles."

NAND

NAND means "not and."[3] To fulfill a NAND, you can have none or some of the options, but not both/all. Example:

NAND: My best friend's cat and my cat hate each other. If we want to have a good time hanging out, we can hang out with neither cat or one cat, but not both. We can hang out with his cat NAND my cat.

When coding, you can write a NAND with a ! (NOT) and &/&& (AND) together. For example, "!(His cat & my cat)"

NOR

NOR means "not or," aka "none of these."[3] Example:

NOR: If I am home alone, then I will set traps for robbers.[4] My mom, my dad, and my brother should not be here if I'm setting traps for robbers. If even one of them is here, no traps!

When coding, you can write a NOR with a ! (NOT) and an | or || (OR). For example, "!(Mom OR Dad OR my brother)"

XNOR

XNOR means "none or all, but not just one."[3] Example:

XNOR: My nephews, Dennis and Linus, love it when I bring them presents after I travel. If I want to be fair, I can have presents for none or presents for both, but not presents for one and not the other. I can bring presents for Dennis XNOR Linus.

When coding, you can make an XNOR with a ! (NOT) and an XOR (^) together. For example, "!(Dennis ^ Linus)"

Complex expressions

You can use parentheses to write more complex expressions.[2][5] Just like in math, you evaluate what's inside the parentheses first. Example:

Doggy playdate: I want my dog to have a playdate. I can invite my cousin's dog or my best friend's dog. My dog's playdate requires: (Cousin's dog | best friend's dog) & my dog.

Another example:

Kitty playdate: My best friend's cat will have a playdate. He can invite his big sister's cat or his girlfriend's cat. My cat hates his cat, so my cat can't come, or it'll be ruined. A kitty playdate for my best friend's cat requires: His cat & (his sister's cat | his girlfriend's cat) & !(my cat).

It can get highly complicated if you want it to. But usually, expressions stay fairly simple.

See also

References