x,y=10,20
which are equivalent, or in a more descriptive manner:
LET numberA,numberB,stringA$,stringB$ =
1,2,"hello","goodbye"
One, extremely handy functionality of assignments is that in the case of assignment of multiple variables, if there are fewer values after the = operator than the variables before it, then all the remaining variables get initialised to that specific value. The following line:
a,b,c,d,e,f=0
will create variables a, b, c, d, e and f and set them all to 0. Similarly the following line:
a,b,c,d,e,f=10,20,30,1
will assign 10 to variable a, 20 to variable b, 30 to variable c and 1 to variables d, e and f. In the example above we could remove altogether line 10 and instead enter:
15 a,b=10,15
which is functionally equivalent to both lines 10 and 15!
Finally, assignments can be made cumulative with the use of special combination operators as seen in the table below:
| Assignment Operator | Numeric Variables | String Variables |
|---|---|---|
| += | Increases variable by the value assigned | Concatenates string variable with the string assigned |
| −= | Decreases variable by the value assigned | – Not Applicable – |
| *= | Multiplies variable by the value assigned | Replicates the string variable as many times as the numeric value assigned |
| /= | Divides variable by the value assigned | – Not Applicable – |
| ^= | Raises variable to the power assigned | – Not Applicable – |
| &= | ANDs variable with the value assigned | – Not Applicable – |
| |= | ORs variable with the value assigned | – Not Applicable – |
| ^|= | XORs variable with the value assigned | – Not Applicable – |
| <<= | Shifts left the variable as many positions as the value assigned | – Not Applicable – |
| >>= | Shifts right the variable as many positions as the value assigned | – Not Applicable – |
| MOD= | Performs a MODulo operation on the variable with the value assigned | – Not Applicable – |
Table 2 – Accumulation assignments
This allows us to be a bit more terse when writing a program by reducing the amount of text we have to type, making for some more readable code. Consider the following example:
10 LET a=10
15 LET b=15
20 LET c=a
30 LET a=a+b
40 LET b=b+c
Using the information we've just learned, we can rewrite it to use multiple assignment statements as well as cumulative assignment operators like so:
ZX Spectrum Next User Manual, 3rd Edition (ISBN 978-1-5272-5496-1), written and illustrated by Phoebus R. Dokos. Copyright © 2020-2024 Phoebus Dokos / SpecNext Ltd. Licensed under CC BY-NC-SA 4.0. This is a transcription and can contain errors; check any doubt against the printed page.