Sums and comparisons
Arithmetic
PRINT 7 + 2, 7 - 2, 7 * 2
PRINT 7 / 2 // dividing always gives a fraction
PRINT 7 \ 2 // a backslash divides and keeps the whole part
PRINT 7 MOD 2 // what's left over
PRINT 2 ^ 10 // to the power of
PRINT SQRT(16), INT(3.9), ABS(-5), MIN(4, 2, 8), MAX(4, 2, 8)
9 5 14
3.5
3
1
1024
4.0 3 5 2 8
From BASIC: These are the ones you know:
\,MODand^.
From Python:
\is your//,MODis your%, and^is your**.
From JavaScript:
^is a power here, and your XOR is~.MODis your%.
From Lua:
\is your//andMODis your%.
Comparing
One = does both jobs. At the start of a line it gives a variable a value.
Anywhere else it asks whether two things are equal.
score = 10
PRINT score = 10, score <> 10
PRINT score > 5, score >= 10, score < 5, score <= 9
PRINT "apple" < "banana"
1 0
1 1 0 0
1
A comparison gives 1 for yes and 0 for no.
From Python, JavaScript and Lua: There’s no
==. Equal is=and not equal is<>.
AND, OR, NOT
They’re words. 0 and NULL count as no; other numbers count as yes.
lives = 3
shield = FALSE
PRINT lives > 0 AND NOT shield
PRINT lives = 0 OR shield
PRINT NOT NULL
1
0
1
From BASIC:
AND,ORandNOTwork on yes and no here. The bit operators are below.
From JavaScript:
ANDandORgive 1 or 0. They don’t hand back one of their two values, soname OR "nobody"won’t give you a default.
From Lua: The same goes for you:
x OR ygives 1 or 0.
Bits
When you need them, they’re symbols: & and, | or, ~ exclusive or (and,
with one value, not), << and >> to shift.
flags = 0b1010
PRINT flags & 0b0110, flags | 1, flags ~ 0b1111
PRINT 1 << 4, 256 >> 2, ~0
2 11 5
16 64 -1