Compound Assignment
This feature is simpler in Scriptol than in any other language. This designates the case you want to assign a variable, an expression that modifies the same variable. For example, adding 10 to x and putting the result into x.Rather than writing x = x + 10, write simply: x + 10
In a conditional test, x + 10 returns just the result of 10 added to the content of x, without to modify x. But if this is a statement, the result will be assigned to x.
Compound assignments may work with these operators:
+
- * / mod << >> & | ^ |
addition substraction multiplication division modulo binary left shifting binary right shifting binary and binary or binary complement |
Examples:
x + 1 ...increments the content of x by 1.
x * y ...replaces the content of x by the result of x * y.
a & b ...replaces the array a by the intersection of a and b.
Example of compound assignment same as: x = x + 10 |
int
x = 10 x + 10 print x |
Display: | > 20 |
This is not a compound assigment, x is not modified. | int
x = 10 print x + 10 print x |
Display: | >
20 > 10 |
Exercises |
1) Here is a list of assignments. Write them in the shortest form
possible. x = x + 10 y = y * 2 x = x / (y + 38) Answer |