7. Assignment and arithmetic
| Meaning | Readable form | Compact form |
|---|---|---|
| Assign | set score to 10 | score = 10 |
| Add | add 5 to score | score += 5 |
| Subtract | subtract 1 from score | score -= 1 |
| Multiply | Use compact form | score *= 2 |
| Divide | Use compact form | score /= 2 |
| Reverse a Boolean | toggle ready | ready = not ready |
function run()
let score = 10
add 5 to score
score *= 2
let average = score / 4
let remainder = 11 % 3
return [score, average, remainder, -score]
end+, -, *, / and % mean add, subtract, multiply, divide and integer remainder. Division returns a number with a fractional part when necessary. % works with integer values. Avoid a zero divisor. There are no
++, --, exponentiation or bitwise operators in PCA Script 1.0.
+ also joins text, and joins two arrays into a new array. If either operand is text, the other is converted to text. Add parentheses when a line mixes text and arithmetic: "Score: " + str(score +
bonus) makes your intention clear.
Assignment uses =. Equality uses == or is. Accidentally writing an assignment where you meant a test is a good reason to validate before you press Run.
