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.

Understanding "logical OR" and "logical AND" in programming languages

+3
−0

I'm posting this as a language-agnostic catch-all for a simple class of logical errors, so that beginner questions can be duplicated to it in the future. In my experience, the large majority of questions along these lines are asked by new Python programmers, but in principle the question applies to many languages.

This question is meant to be focused specifically on the theoretical understanding of the logical flaw - it is not about details of what the correct syntax looks like (only a language-agnostic description), and it is especially not about other ways to solve underlying problems.

Many programming languages - most notably Python, but the question applies generally - either have keywords like or and and used for logic, or equivalent operators such as || or && - which are referred to as "logical or" and "logical and" respectively in the language documentation. However, these tools don't seem to work in a natural or expected way for many beginners. Which is to say: "translating" an English sentence into code, using or or || to represent the English word "or", etc., commonly causes errors or gives the wrong result.

Why is this? What are the semantics of these operators, and how is that different from a natural-language understanding? And why are they called "logical"? (Are there other kinds?)

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

1 answer

+3
−0

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 - more info here: Understanding "de Morgan's laws".


As for why they are called logical, that goes back to 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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »