Math for Programming / Part 2: Math inside the computer

Logic: how programs make decisions

Every “if” in a program is a small piece of logic. A few rules about and, or, and not make those decisions precise and easier to get right.

Lesson 10 of 13About 10 minutesBuilds on: inequalities

True or false

A program makes decisions with statements that are either true or false. These are called Boolean values, after the mathematician George Boole. Comparisons produce them: score > 100 is true or false; so is lives == 0. Three operations combine them:

ABA and BA or Bnot A
falsefalsefalsefalsetrue
falsetruefalsetruetrue
truefalsefalsetruefalse
truetruetruetruefalse

This is a truth table. It lists every possible input, so it is a complete proof of how each operation behaves. Note that “or” in programming means “at least one,” so true or true is true.

Figure

Switch A and B and watch the results.

Turning rules into conditions

“The door opens if the player has the key and is standing next to it, unless the alarm is on” becomes has_key and near_door and not alarm_on. Writing a rule this precisely is a mathematical skill: everyday language is often ambiguous, while logic is not.

Worked example: is the point inside the rectangle?

A button covers x from 100 to 250 and y from 40 to 90. A click at (180, 60) is inside if 100 <= x and x <= 250 and 40 <= y and y <= 90. All four are true, so yes. A click at (300, 60) fails the second test, so no.

Do two rectangles overlap?

Collision checks in simple games compare boxes. Two rectangles overlap when each one starts before the other ends, on both axes:

GDScript
func overlaps(a: Rect2, b: Rect2) -> bool:
    return a.position.x < b.end.x and b.position.x < a.end.x \
       and a.position.y < b.end.y and b.position.y < a.end.y
# Godot also provides a.intersects(b)

It is often easier to think about the opposite: the boxes miss when one is entirely to the left, right, above, or below the other. Flipping between “overlap” and “miss” uses the next idea.

De Morgan’s laws

Two rules tell you how “not” passes through “and” and “or”:

not (A and B) = (not A) or (not B)
not (A or B) = (not A) and (not B)

In words: “not both” means “at least one is missing,” and “neither” means “both are missing.” You can confirm each law by checking all four rows of a truth table. Programmers use them to simplify conditions and to fix bugs where a “not” was applied to only part of a rule.

Common mistake

Writing if x == 1 or 2: to mean “x is 1 or 2.” Many languages read it as (x == 1) or (2), and since 2 counts as true, the condition is always true. Write each comparison in full: x == 1 or x == 2.

Where this comes from in school math

Compound inequalities from Algebra I, such as 3 ≤ x < 7, are “and” statements, while “x < 0 or x > 10” is an “or.” Geometry proofs use the same logic: if-then statements, converses, and counterexamples. A truth table is a proof by checking every case.

Where you will see it

  • Game rules: when a door opens, a level ends, or an enemy attacks.
  • Collision: overlapping boxes and points inside shapes.
  • Search: filters such as “price under $20 and in stock.”
  • Hardware: computer chips are built from millions of AND, OR, and NOT gates.

Exercises

Simplify not (is_raining or is_cold) with De Morgan’s law.

not is_raining and not is_cold: it is neither raining nor cold.

Box A spans x from 0 to 4. Box B spans x from 3 to 6. Do they overlap horizontally?

Yes: 0 < 6 and 3 < 4.

Write a condition for “age is at least 13 but under 18.”

age >= 13 and age < 18.