Skip to content
Varlig Guide
Varlig home

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 ², ³ 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:

calc
£12.50 + £4.30 gives £16.80
3 * £4.50 gives £13.50
£1,000 * 1.05^3 gives £1,157.63
6 times 7 gives 42
100 minus 37.5 gives 62.5

Juxtaposition means leaving out the *, as in 2(3 + 4) or . Implicit multiplication explains when that works and when it doesn’t.

Order of operations

Calculations follow the usual order from school maths:

  1. Brackets.
  2. Factorials, such as 5!.
  3. Powers, worked out from right to left: 2^3^2 is 2^9, which is 512.
  4. A leading minus sign, applied after the power: -2^2 is -4, while (-2)^2 is 4.
  5. Multiplication, division and mod, from left to right.
  6. 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:

calc
£85 - £12.50 * 2 gives £60.00
(£85 - £12.50) * 2 gives £145.00
£120 / 4 / 2 gives £15.00

When 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:

calc
(9 + 16) gives 5

Remainders

mod gives what is left over after dividing. It is useful for splitting things into groups:

calc
eggs = 50
eggs mod 6 gives 2
floor(eggs / 6) gives 8

Fifty 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:

calc
5! gives 120
runners = 4
runners! gives 24
3! + 2 gives 8
5!=4 gives true

A 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:

calc
budget = £400
spent = £365
spent <= budget gives true
spent == budget gives false
spent != budget gives true
5 km == 5,000 m gives true
90 min >= 1.5 hours gives true
2 kg < 2,500 g gives true

Units 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 < 10 is 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:
calc
deadline = 30 September 2026
booked = 14 September 2026
deadline - booked gives 2 weeks 2 days
deadline - booked >= 7 days gives true

Logical 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.

calc
distance = 5 km
run time = 27 min
distance >= 5 km && run time < 30 min gives true
distance >= 10 km || run time < 25 min gives false
distance >= 10 km or run time < 30 min gives true

true and false can be written directly and combined with && and ||:

calc
true || false gives true
true && false gives false

Some forms you might expect are not supported:

  • and and or only work between comparisons. true and false doesn’t work; write true && false.
  • There is no negation operator: not true and !true don’t work. Compare with false instead, as in (spent > budget) == false.
  • or is not a fallback for errors: 1/0 or 5 doesn’t give 5.

Using comparisons in conditionals

A comparison is most useful inside if … then … else …, which picks a value depending on the answer:

calc
basket = £34.50
if basket >= £40 then £0 else £3.99 gives £3.99

Conditionals 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.

calc
12 & 10 gives 8
12 | 10 gives 14
12 xor 10 gives 6
12 AND 10 gives 8
1 << 10 gives 1,024
1024 >> 3 gives 128

The 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:

calc
0o644 | 0o111 to octal gives 0o755
colour = 0xFF8800
(colour >> 8) & 0xFF gives 136

Shifts 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:

calc
# September budget
take home = £2,400
rent = £1,050
bills = £230
groceries = 4 * £65 gives £260.00
left over = take home - rent - bills - groceries gives £860.00
rent / take home as % gives 43.75%
left over > £500 gives true
left over > £500 && rent <= take home * 45% gives true

groceries 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.