Dangerous Logic - De Morgan & Programming
Written by Mike James
Tuesday, 28 October 2025
Article Index
Dangerous Logic - De Morgan & Programming
Negation and De Morgan's Law
Page 1 of 2

Programmers are master logicians - well they sometimes are. Most of the time they are as useless at it as the average joe. The difference is that the average joe can avoid logic and hence the mistakes. How good are you at logical expressions and why exactly is Augustus De Morgan your best friend, logically speaking?

It is commonly held that programming is a logical subject.

Programmers are great at working out the logic of it all and expressing it clearly and succinctly, but logic is tough to get right.

IFs and Intervals

A logical expression is just something that works out to be true or false.

Generally you first meet logical expressions as part of learning about if statements. Most languages have a construct something like

if condition then other instructions

or just

if (condition) other instructions

Usually the conditions that you first meet are things like

if(A>0)

or

if(A==0)

At this stage the only real difficulty you are likely to have is with the use of strange symbols to replace the usual mathematical ones like == for equality, <= for less than or equal to and so on.

Languages have never standardized on what symbols to use, but most use something like:

Symbol Meaning
= or == equals
<= less than or equal to
>= greater than or equal to
!= or <> not equal

A little later on you discover that you need to create compound conditionals that involve comparing multiple values.

For example

if(A>=0 AND A<=10)

clearly this means that both conditions have to be true. In the same way

if(A>=0 OR A<=10)

means that either condition has to be true.

You might regard both of these as obvious, but such logical compounds have a meaning in terms of the range of values they allow. That is, these compound conditions define intervals that A has to be in for the condition to be true.

The first if statement means that A has to be in the range 0 to 10. Easy.

What about the second if statement?

Surely it is easy, after all we have only changed an AND to an OR?

The answer is that the first condition says that A has to be non-negative, i.e. greater than or equal to zero. The second condition is that A has to be less than or equal to 10.

Put the two together and you find that one of them is true for any value of A - so the condition is true for all A.

This looks trickier than it first seems.

How can we check what interval corresponds to true?

The answer is very simple.

Each condition that you write down specifies a region of the number line e.g. A>=0 is the section to the right of the zero and A<=10 is the section to the left of 10:

[画像:intervals1]

Put the two together with an AND and the region where they are both true is the overlap:

[画像:intervals2]

Same reasoning applies if you put them together with OR only in this case the area where one or more of them is true is the entire colored area:

[画像:intervals3]

In other words the AND of the two intervals is the intersection of the areas that they cover and the OR is the union of the areas.

Now where have you heard this before?

The answer is in a Venn diagram.

In this case the areas are usually drawn as circles but the rules for overlapping are the same - AND is where they intersect and OR is the total area they cover.

[画像:venn]

Intervals in logical conditionals work in the same way.

Next question what is the NOT of an interval?

Easy its just the area it doesn't cover just as in the case of the Venn diagram:

[画像:intervals4]

It is the NOT of an interval that often causes trouble. Notice that the NOT of a less than or equal to is just greater than i.e. no equals. Similarly the NOT of a greater than is a less than or equal to. You just have to remember to drop or add the final point at the start of the interval.

It's all obvious really - but how many times do you see it pointed out or explained in this way?


Prev - Next >>

Last Updated ( Tuesday, 28 October 2025 )