Most of these operators have appeared already in this course, often used to support example code. For completeness, however, here is a complete list:
Brackets are used to override the usual 'precedence' rules, the order in which operators are applied. For instance, 1 + 2 * 3
would normally evaluate to 7
, but (1 + 2) * 3
evaluates to 9.
If a +
is put before a single operand, it simply means 'positive', so +4
is the same as 4
.
If it appears between two values, it adds them. 4 + 2
evaluates to 6
If a -
is put before a single operand, it simply means 'negative', so -4
is simply 'minus 4'.
If it appears between two values, it substracts the second from the first. 4 - 2
evaluates to 2
4 * 2
evaluates to 8
.
4 / 2
evaluates to 2
.
4 ^ 2
and 4 ** 2
both evaluate to 16
.
'Hello ' : 'World!'
and 'Hello ' CAT 'World!'
both evaluate to Hello World!
.
To extract n characters from the end of a string, append [n]
. 'UniVerse'[2]
evaluates to se
.
To extract n characters starting from position p, append [p,n]
. 'UniVerse'[3,2]
evaluates to iV
.
To break your string into sections delimited by character c, and take n sections beginning from section s, append [c,s,n]
. 'UniVerse Basic Programming Language'[' ',2,1]
evaluates to Basic
.
1 = 2
evaluates to 0
(falsity).
1 # 2
evaluates to 1
(truth).
1 < 2
evaluates to 1
(truth).
1 > 2
evaluates to 0
(falsity).
1 <= 2
evaluates to 1
(truth).
1 >= 2
evaluates to 0
(falsity).
Use this operator to check a string against a 'pattern template'. These templates are made up of a number of elements strung together. Each element takes the form nt, where n is a number between 0 and 9, representing 'number of characters' (0 meaning 'any number of characters) and t is a 'type of characters': A for alpha, N for numeric, and X for 'any'. Thus the pattern template 2A
means 'two alpha characters, 0N
means 'any number of numeric characters', and 2X1A
means 'any two characters followed by an alpha character'. A few examples may clarify. All of the following evaluate to 1
(truth):
'ABC123' MATCHES '3A3N'
'Route 66' MATCHES '0X2N'
'Cloud 9' MATCHES '5A1X1N'
You can also include literals in your pattern template, putting them in quotes if they might otherwise be mistaken for pattern elements. My own advice is to use quotes to highlight literals whether or not they are required. All these are true too:
'ABC123' MATCHES '"ABC"3N'
'Route 66' MATCHES '"Route "2N'
'Cloud 9' MATCHES '0X"9"'
Fin her expression could be used. C programmers may recognise this as a form of their beloved condition ? truevalue : falsevalue
operator.
AND
combines two conditions, and evaluates to 1
(truth) if they are both true, or 0
(falsity) if either or both are not. 1 = 2 AND 3 # 4
evaluates to 0
(false).
OR
combines two conditions, and evaluates to 1
(truth) if either or both are true, or 0
(falsity) if they are both false. 1 = 2 AND 3 # 4
evaluates to 1
(truth).
NOT
is actually a function, but is so closely related to AND
and OR
that it is always documented with them. NOT
takes a single parameter, returning 1
(truth) if it is false, or 0
(falsity) if it is true. NOT(1 = 2)
evaluates to 1
(truth).
The assignment of variables is straightforward, taking the form VARIABLE = VALUE
.
To increase the numeric value stored by a variable, use VARIABLE += INCREMENT
. This is logically the same as VARIABLE = VARIABLE + INCREMENT
, but is more efficient and clearer.
Similarly, you can decrease the numeric value stored by a variable using VARIABLE -= DECREMENT
.
To concatenate a string to the end of an existing string, use VARIABLE := EXTRA.BIT
.
The following example code uses all four assignment operators.
VALUE = 100 VALUE += 1 ; * Value now 101 VALUE -= 2 ; * Value now 99 VALUE := '!' ; * Value now 99!