What are operators? |
Topics | |
1. Generalities
2. Math operators
3. String operators
1. Generalities
The operators are the characters or signs you use
in expressions like 2+3 or 2*a + b, etc...
to carry out different operations.
Each operator has a specific priority, for
example multiplications take
precedence over additions.
Expressions are evaluated from left to right.
The dollar sign '$' must be placed at the
end of an expression to indicate that the the expression
must be evaluated in a string context.
Not like: if a$ = "hello" and b$ = "world"
but like: if a = "hello" and b = "world"$
If you need both contexts in the same expression,
the way to do it is to use parentheses:
if (a = "hello"$) and c = 7
Priority 1 = done first, 8 = done last |
Operator | Use |
---|---|---|
1 | ^ | Exponent. p = 2 ^ 3, p contains 8 |
2 | * | Multiplication. m = 4 * 3, m contains 12 |
2 | / | Division. d = 10 / 3, d contains 3.3333 |
2 | % | Modulo, that is the remainder of a division of an integer by another integer.
If you use floating point values, they will first be transformed into integers. m = 10 % 3, m contains 1 |
3 | + | Addition a = 10 + 4, a contains 14 |
3 | - | Substraction. s = 5 - 3, s contains 2 |
4 | & | Binary and. b = 3 & 2, b contains 2 |
4 | | | Binary or. o= 5 | 3, o contains 7 |
6 | < | 'Less than'. l = 5 < 3, l contains 0 l = 1 < 2, l contains 1 This operator is mainly used in tests, like in: 'if a < b' |
6 | > | 'Greater than'. g = 5 > 3, g contains 1 g = 1 > 2, g contains 0 This operator is mainly used in tests, like in: 'if a > b' |
6 | <= | 'Less than or equal to'. s = 5 <= 3, s contains 0 s = 1 <= 2, s contains 1 This operator is mainly used in tests, like in: 'if a <= b' |
6 | >= | 'Greater than or equal to'. b = 5 > = 3, b contains 1 b = 1 > = 2, e contains 0 This operator is mainly used in tests, like in: 'if a >= b' |
7 | != Or <> |
Different of. d = 1 != 2, d contains 1 d = 1 != 1, d contains 0 This operator is mainly used in tests, like in: 'if a != b' |
7 | = | Equality. This operator, which is the same as the assignment one, is mainly used in tests, like in: 'if a = b' |
8 | and | logical and. Gives 1 if the two operands are not 0. A = 5 and 2, A contains 1 A = 5 and 0, A contains 0 This operator is mainly used in tests, 'if a > 2 and a < 10' meaning 'if a is between 2 and 10 excluded' |
8 | or | logical or. Gives 1 if either one the two operands is not 0. R = 5 and 3, R contains 1 R = 5 and 0, R contains 1 R = 0 and 0, R contains 0 This operator is mainly used in tests like in: 'if a < 2 or b < 2'. |
3. String operators
In a string context, some of the operators
mentionned above don't apply. Others are used
in a different way.
String operators that do comparisons return the string
'1' when the comparison is successful, otherwise they
return an empty string.
Priority 3 = done first, 8 = done last |
Operator | Use |
---|---|---|
3 | + | Concatenation a = "hello" $ b = "world" $ c = a + " " + b $, c$ contains "hello world" |
6 | < | 'Less than'. Comparison on the length of the strings |
6 | > | 'Greater than'. Comparison on the length of the strings |
6 | <= | 'Less than or equal to'. Comparison on the length of the strings |
6 | >= | 'Greater than or equal to'. Comparison on the length of the strings |
7 | != Or <> |
Different of. This operator compares the two strings letter by letter and is case-sensitive. |
7 | = | Equality. This operator compares the two strings letter by letter and is case-sensitive. |
8 | and | logical and. "1" is returned if both strings are not empty. |
8 | or | logical or. "1" is returned if either one of the two string is not empty. |