Operators and Expressions

⏱ 8 min✏️ Quiz at the end

The Arithmetic You Already Know, Renamed

Programs calculate constantly β€” scores, prices, positions, averages. The calculating is done by operators acting on values, and any combination that boils down to a single result is an expression:

price * 0.8            (20% off)
(a + b + c) / 3        (average of three)
seconds MOD 60         (the seconds left over after whole minutes)

Expressions live everywhere: on the right of an assignment, inside an IF condition, between the brackets of a function call. Learning to read them fluently is learning to read code.

Arithmetic Operators

Four are old friends with new clothes β€” +, βˆ’, * (multiply), / (divide) β€” keyboards having no Γ— or Γ·. Two more earn their keep in programming:

  • MOD β€” the remainder after whole-number division. 17 MOD 5 = 2 (17 Γ· 5 is 3, remainder 2).
  • DIV β€” the whole-number quotient itself. 17 DIV 5 = 3.

MOD looks humble and is everywhere. Is n even? n MOD 2 = 0. Convert 137 seconds to minutes and seconds? 137 DIV 60 = 2 and 137 MOD 60 = 17 β€” 2:17. Make a game character wrap from position 9 back to 0 on a 10-square board? position MOD 10. Anything that cycles has MOD hiding in it β€” a pattern worth recognising. (MOD is also doing the work when you convert to binary by repeated division.)

Precedence: Who Goes First

2 + 3 * 4 β€” is it 20 or 14? Programming languages answer exactly as your maths teacher does: multiplication and division before addition and subtraction, so it is 14. The ordering rules are called precedence, and brackets override them: (2 + 3) * 4 = 20.

The professional habit: when in doubt, bracket. (a + b) / 2 costs two characters and removes all ambiguity β€” for the computer and for the human reading your code next week. A misplaced precedence assumption is a quiet bug: no crash, just wrong numbers.

Comparison Operators: Expressions That Answer Questions

A second family of operators compares values β€” and their result is not a number but a Boolean: true or false.

OperatorMeaningExample (x = 10)
= or ==equalsx == 10 β†’ true
β‰  or !=not equalx != 10 β†’ false
< , >less / greater thanx < 10 β†’ false
<= , >=at most / at leastx <= 10 β†’ true

These are exactly what conditions are made of: IF age >= 13 THEN … works because age >= 13 evaluates to true or false first, and the IF acts on that value. Chain comparisons with AND, OR, and NOT and you can express any rule: (age >= 13) AND (age < 20) β€” a teenager.

One trap deserves its warning label: in many languages = assigns while == compares. Write x = 5 where you meant x == 5 and, in some languages, you set x to 5 instead of testing it β€” a legendary bug with decades of victims.

Evaluating Like a Machine

To find an expression's value, work inside-out and by precedence β€” the same discipline as a trace table. With x = 7:

(x MOD 2 = 1) AND (x > 5)
β†’ (7 MOD 2 = 1) AND (7 > 5)
β†’ (1 = 1) AND (true)
β†’ true AND true
β†’ true

x is odd and bigger than five. One line of code, four small steps, zero mystery. Every expression a computer ever evaluates β€” including the monsters β€” unwinds this same way.

Try It Yourself

With n = 24, evaluate by hand: n MOD 5, n DIV 5, n MOD 2 = 0, (n > 20) AND (n MOD 3 = 0). Then write an expression that is true exactly when a year number is a leap year candidate: divisible by 4. (Bonus, for the brave: divisible by 4, AND NOT divisible by 100 unless also by 400 β€” real calendar code!)

Worked Example β€” Building a Real Expression Step by Step

A theme park ride needs a single expression deciding who may ride: height over 120cm, AND (age at least 8, OR accompanied by an adult). Building this correctly, piece by piece, avoids the common mistake of guessing at the whole thing in one go:

canRide ← (height > 120) AND ((age >= 8) OR hasAdult)

Evaluate it for a 115cm, 6-year-old accompanied by an adult:

(height > 120) AND ((age >= 8) OR hasAdult)
β†’ (115 > 120) AND ((6 >= 8) OR true)
β†’ false AND (false OR true)
β†’ false AND true
β†’ false

The child cannot ride β€” correctly, since the height requirement alone already fails, and AND needs every part true regardless of how generously the other side is satisfied. This mirrors exactly how Boolean logic combines conditions, but shows the extra ingredient this lesson adds: comparison operators (>, >=) generating the true/false values that AND and OR then combine. Reading a nested expression like this one, one bracket at a time, is a skill that transfers directly to reading real code, pseudocode conditions, and nested selection alike.

A Common Precedence Trap: Comparisons and Boolean Operators Together

When comparisons and AND/OR mix in one expression, precedence rules decide which happens first, and getting this wrong is a genuine, recurring bug. Consider:

age >= 13 AND age < 20

This is evaluated as (age >= 13) AND (age < 20) β€” comparisons are worked out before AND combines their results β€” which is what was intended: a teenager. But a careless rewrite like age >= 13 AND < 20 is not valid at all in most languages, precisely because AND needs two complete true/false values on either side, not a bare number on one side. When expressions get this complex, the earlier advice bears repeating with extra force: bracket generously β€” (age >= 13) AND (age < 20) costs nothing and removes any doubt about what is being compared with what.

Key Words

  • Operator β€” a symbol performing an operation: + βˆ’ * / MOD DIV, and comparisons
  • Expression β€” values, variables, and operators combining into one result
  • MOD / DIV β€” remainder / whole-number quotient after division
  • Precedence β€” the order operations happen; brackets override it
  • Comparison operator β€” produces a Boolean: = β‰  < > <= >=