Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

77%
+5 −0
Q&A Understanding "logical OR" and "logical AND" in programming languages

They aren't actually that different from natural language. But more verbose. Given some generic pseudo code looking like one of the C family languages: if( !egg.boiled || !egg.peeled ) { do_no...

posted 1mo ago by Lundin‭

Answer
#1: Initial revision by user avatar Lundin‭ · 2024-11-14T16:29:00Z (about 1 month ago)
They aren't actually that different from natural language. But more verbose. Given some generic pseudo code looking like one of the C family languages:

```
if( !egg.boiled || !egg.peeled )
{
  do_not_eat(egg);
}
```

Then this is to be translated as:  
_"If the egg is not boiled or the egg is not peeled, don't eat it."_

In everyday English we would probably rather say:  
_"If the egg isn't boiled or peeled don't eat it."_

Confusion arrises because we don't repeat the noun over and over in everyday English. We wouldn't say "If the egg isn't boiled or the egg isn't peeled or the egg isn't smashed", we would say "if the egg isn't boiled, peeled or smashed".

And so beginners may think they can write code like `if (a == 1 || 2 || 3)` because that's how you'd say it "in your head" when writing the code.

But programming languages usually demand that we are more verbose and refer to the object in every sub expression. In this example the equality `==` operator is the one doing the comparison and that one is defined by the language syntax to take two operands. As are the `||` logical OR operators. 

We just have to fall in line and follow the language syntax, which isn't so much there for the convenience of the human brain as for the compiler implementers having a fighting chance to understand the programmer's intention. Consider for example `if (a == 1 || 2 || b == 3)`, we can't just guess which variable the `2` is to be compared with.

Similarly, most languages do not support "interval syntax" as seen in math: `0 < i < n`. Here too we have to use operands with two operators for each `<` and in order to glue them together, an additional AND: `if (0<i && i<n)`. Or perhaps more readable for a programmer, keep the changing variable to the left side consistently: `if (i>0 && i<n)`.

---

Notably, if we use common sense and turn the wording around in our head:
_"If the egg is not boiled or the egg is not peeled, don't eat it."_ 
 
We might simply just say:    
_"If the egg is not boiled and peeled, don't eat it"_  

Note the change from `or` to `and` here. The new wording has the same meaning, but only if `egg` refers to `not (boiled and peeled)`. From this we can conclude that

`(the egg is not boiled) or (the egg is not peeled)`

is equivalent to

`the egg is not (boiled and peeled)`.

If it is either not boiled nor peeled, then it can't very well be boiled _and_ peeled.

By just using common sense we have actually applied what Boolean algebra refers to as  
[De Morgan's laws](https://en.wikipedia.org/wiki/De_Morgan%27s_laws) - more info here: [Understanding "de Morgan's laws"](https://software.codidact.com/posts/292969). 

---

As for why they are called logical, that goes back to [digital electronics](https://en.wikipedia.org/wiki/Digital_electronics), which uses terms that go back to the mathematician George Boole and the term logic ultimately originates from classic Greek, math and philosophy. Before there were computers, there were _logic gates_, which are electronic components in the form of integrated circuits based on transistors.

For example an AND gate is an integrated circuit where you could enter two voltages on two pins and if both voltages were high, you would get a high voltage out. Otherwise a low voltage. You'd describe the behavior of the gate with something known as a _truth table_, here illustrating one for an AND gate:

```
input1  input2  output
  0       0        0
  0       1        0
  1       0        0
  1       1        1
```

0 means low voltage and 1 means high voltage. Or if you will: 0 is false and 1 is true. So this directly corresponds to `and`/`&&` operators in programming. In fact the schematic IEC symbol for an AND gate is a `&`.

---

Some programming languages do in fact have other forms of AND/OR than the logical ones. Low-level languages like C have another category of operators that work on a bit level rather than Boolean level. They are called _bit-wise_ operators and low-level languages typically have a bunch of them: bit-wise AND `&`, bit-wise OR `|`, bit-wise XOR `^` and so on. Also there are bit shift operators, moving all binary digits _n_ steps to the left or right. 

I won't go into how these work here as there's plenty of learning material out there. Also these come with various language-specific quirks to them.