Operators
Operators combine some values to new values. Operators have a certain
precedence, that is, some operators are evaluated before others, so
you can write expressions in the intuitive fashion:
10+5*3
Cost.Mc + Cost.Supplies < Supplies
Here, `*' takes precedence over `+' so the multiplication is evaluated
before the addition, and `+' has precedence over `<'.
You can use parentheses to override precedence.
This page lists operators in decreasing order of precedence, that is,
the highest-precedence operators come first.
a^b
|
|
computes `a' to the `b'th power. `a' must be a number, `b' an
integer.
|
-a
|
|
negates the number `a'.
|
+a
|
|
exists for symmetry to the `-' operator.
Example: -3^2 = -9, (-3)^2 = 9
|
Multiplication and Division |
a*b
|
|
multiply the numbers `a' and `b'
|
a/b
|
|
divide `a' by `b', yielding a real number
|
a\b
|
|
integer division. Discards the remainder
|
a MOD b
|
|
returns the remainder `a' yields when divided by `b'
Examples:
17 / 5 = 3.4
17 \ 5 = 3
17 MOD 5 = 2 |
|
a+b
|
|
add the numbers `a' and `b'. If both operands are strings,
concatenates (appends) them. However, I recommend you using
the `#' or `&' operator for appending strings instead.
|
a-b
|
|
subtract the numbers `a' and `b'
|
a & b
|
|
convert the values `a' and `b' to strings (by means of the
Str(xx) function), and append them. EMPTY values
are treated as empty strings.
|
a # b
|
|
similar to the above operator, but EMPTY values cause the
return value to be EMPTY.
Example:
Hull.Mass & " kt" | yields for example "100 kt", or " kt" if the mass is not known |
Hull.Mass # " kt" | yields "100 kt", or EMPTY |
|
a < b, a > b, a = b, a <= b, a >= b, a <> b
|
|
compare their arguments `a' and `b', which must be both numbers, or
both strings. Yields `True' or `False'.
Strings are compared for their relative position in a dictionary.
`Special' characters are sorted as in the normal ASCII set, that is,
`"<" < "A"', `"[" > "Z"', etc. By default, strings are compared
case insensitive ("a" = "A"); to change this, use the
StrCase() function.
Examples:
"a" = "A" % true
StrCase("a" = "A") % false
17 < 42 % true
9 > "a" % error |
If an expression is used as a statement, the "=" operator at top-level
is automagically turned into the assignment operator ":="
(not in an expression for printing, searching, labelling!). "top-level"
means that "=" is the last operator executed:
a = b | top-level |
a = 10+9 | top-level |
a=1 and b=2 | not top-level (`And' of two comparisons) |
If a=b Then Print "x" | `a=b' is not a statement |
To cut a long story short: PCC attempts to do what you mean.
|
NOT a
|
|
yields `True' if `a' is false (zero, empty string), `False'
otherwise. EMPTY values yield EMPTY.
|
a AND b
|
|
yields `True' if and only if both `a' and `b' are `True'.
|
a OR b
|
|
yields `True' if one of `a' and `b' (or both) are `True'.
|
a XOR b
|
|
yields `True' if exactly one of `a' and `b' is `True'.
|
a := b
|
|
computes the value of `b', and assigns it to `a'. `a'
must be a variable name. Returns the value of `b'.
Example:
% How much Neutronium do I mine?
Dim Local newn
newn := Round(Mines * Density.N / 100) |
(note: do not use this code; see the `nextturn.q' example instead)
Assignments can be chained:
a := b := 1
will set both `a' and `b' to 1.
|
a; b
|
|
evaluates `a', then `b', and returns the value of `b'.
This is a possibility to execute two assignments where PCC
expects only a single expression.
|
Note: For `And', `Or' and `Xor', PCC uses shortcut evaluation. That is,
the second operand is not evaluated if the result is already known after
looking at the first. This is the case for `And' if the first operand
is `False', for `Or' if the first operand is `True', and for `Xor' if
it is EMPTY. The result is "logically correct", that is,
`Z(0) OR True' yields `True', `Z(0) OR False' yields EMPTY. True ternary
logic.
If a value is EMPTY, other operators and functions may also not evaluate
all their arguments in PCC 1.x. In PCC2, all operators except for
`And', `Or' and `Xor', and all functions, evaluate all their arguments.
[ << Previous | Up | Next >> ]
Stefan Reuther <Streu@gmx.de>