Operators, comparisons and conditionals
This page covers the symbols and words that combine values: arithmetic, the order in which it
is worked out, comparisons that answer true or false, and the logical and bitwise operators.
Reach for it when a line gives a different number from the one you expected, or when you want a
note to check something for you, such as whether you are still within budget.
Arithmetic
| Operator | Spellings | Example | Result |
|---|---|---|---|
| Add | +, plus |
£12.50 + £4.30 |
£16.80 |
| Subtract | -, −, minus |
£20 - £4.50 |
£15.50 |
| Multiply | *, ×, times, multiplied by, juxtaposition |
3 × £4.50, 2(3 + 4) |
£13.50, 14 |
| Divide | /, ÷, divided by |
£60 / 4 |
£15.00 |
| Power | ^, **, to the power of, raised to, exponent |
2 ** 3, 2 raised to 10 |
8, 1,024 |
| Remainder | mod, % (between two numbers) |
50 mod 6, 50 % 6 |
2 |
| Square root | √, sqrt(...), square root of |
√144 |
12 |
| Squared / cubed | ², ³ |
3³ |
27 |
| Factorial | !, factorial(...) |
5!, factorial(5) |
120 |
Arithmetic works on plain numbers, money and quantities with units, and the answer keeps the currency or unit:
£12.50 + £4.30 gives £16.803 * £4.50 gives £13.50£1,000 * 1.05^3 gives £1,157.636 times 7 gives 42100 minus 37.5 gives 62.5Juxtaposition means leaving out the *, as in 2(3 + 4) or 2π.
Implicit multiplication explains when that works and when
it doesn’t.
Order of operations
Calculations follow the usual order from school maths:
- Brackets.
- Factorials, such as
5!. - Powers, worked out from right to left:
2^3^2is2^9, which is 512. - A leading minus sign, applied after the power:
-2^2is -4, while(-2)^2is 4. - Multiplication, division and
mod, from left to right. - Addition and subtraction, from left to right.
Leaving out the * doesn’t change this order, so 2x^2 squares x before doubling it, the
way a textbook formula works.
That means a multiplication happens before an addition next to it, which catches people out when adding up a bill:
£85 - £12.50 * 2 gives £60.00(£85 - £12.50) * 2 gives £145.00£120 / 4 / 2 gives £15.00When in doubt, add brackets. They never hurt, and they make the note easier to read later.
A square root sign applies to the value straight after it, so √9 + 16 is 19. Put the whole
expression in brackets to take the root of a sum:
√(9 + 16) gives 5Remainders
mod gives what is left over after dividing. It is useful for splitting things into groups:
eggs = 50eggs mod 6 gives 2floor(eggs / 6) gives 8Fifty eggs fill eight boxes of six, with two left over.
% between two numbers is also the remainder, even without a space before the second number:
50 % 6 and 50% 6 both give 2. A % with nothing after it makes a percentage (50%), which
is covered in Percentages.
Factorials
Put ! straight after a number, a name or a closing bracket to get its factorial:
5! gives 120runners = 4runners! gives 243! + 2 gives 85!=4 gives trueA factorial is worked out before a power next to it, so 2^3! is 2⁶, which is 64. !
followed by = is still “not equal”, as the last line shows. factorial(5) gives the same
answer as 5!.
Comparison
==, !=, <, <=, > and >= compare two values and answer true or false. They work
with plain numbers, with amounts in the same currency, and with quantities that measure the same
thing, even in different units:
budget = £400spent = £365spent <= budget gives truespent == budget gives falsespent != budget gives true5 km == 5,000 m gives true90 min >= 1.5 hours gives true2 kg < 2,500 g gives trueUnits are converted before comparing, so 1 mile > 1.5 km is true. Comparing things that
measure different quantities, such as kilograms and metres, gives an incompatible units error.
Things comparisons don’t do
- Chains.
5 < x < 10is not supported. Combine two comparisons instead:5 < x && x < 10. - Unicode symbols.
≠,≤and≥are not recognised. Use!=,<=and>=. - Dates. You can’t compare two dates with
<or>. Subtract them and compare the difference with a duration:
deadline = 30 September 2026booked = 14 September 2026deadline - booked gives 2 weeks 2 daysdeadline - booked >= 7 days gives trueLogical operators
Join comparisons with && (both must be true) or || (at least one must be true). The words
and and or work the same way between two comparisons.
distance = 5 kmrun time = 27 mindistance >= 5 km && run time < 30 min gives truedistance >= 10 km || run time < 25 min gives falsedistance >= 10 km or run time < 30 min gives truetrue and false can be written directly and combined with && and ||:
true || false gives truetrue && false gives falseSome forms you might expect are not supported:
andandoronly work between comparisons.true and falsedoesn’t work; writetrue && false.- There is no negation operator:
not trueand!truedon’t work. Compare withfalseinstead, as in(spent > budget) == false. oris not a fallback for errors:1/0 or 5doesn’t give 5.
Using comparisons in conditionals
A comparison is most useful inside if … then … else …, which picks a value depending on the
answer:
basket = £34.50if basket >= £40 then £0 else £3.99 gives £3.99Conditionals covers the full syntax, including else if,
postfix if and unless, and assigning inside a branch.
Bitwise operators
Whole numbers support the bitwise operators & (and), | (or), xor, << (shift left) and
>> (shift right). The uppercase words AND, OR and XOR work too. These are mostly useful
for programming tasks such as colour values and file permissions.
12 & 10 gives 812 | 10 gives 1412 xor 10 gives 612 AND 10 gives 81 << 10 gives 1,0241024 >> 3 gives 128The answer is shown as an ordinary number. Add to binary, in hex or to octal to see it in
the base you are working in:
0o644 | 0o111 to octal gives 0o755colour = 0xFF8800(colour >> 8) & 0xFF gives 136Shifts are worked out before &, | and xor, but brackets make a line like the colour example
much easier to follow.
Putting it together
A monthly budget check that uses arithmetic, comparisons and logic together:
# September budgettake home = £2,400rent = £1,050bills = £230groceries = 4 * £65 gives £260.00left over = take home - rent - bills - groceries gives £860.00rent / take home as % gives 43.75%left over > £500 gives trueleft over > £500 && rent <= take home * 45% gives truegroceries multiplies before anything is added, and left over subtracts from left to right.
The last two lines turn the note into a check: the answer stays true while there is more than
£500 left and rent stays within 45% of take-home pay, and flips to false as soon as either
stops holding.