Compound assignment statements
Gosu supports all operators in the Java language, including bit-oriented operators. Additionally, Gosu has compound operators such as:
++– The increment-by-one operator, which is supported only after the variable name+=– The add-and-assign operator, which is supported only after the variable name followed by a value to add to the variable- Similarly, Gosu supports
--(decrement-by-one) and-=(subtract-and-assign) - Gosu supports additional compound assignment statements that mirror other common operators.
For example, to increment the variable i by 1:
i++These operators always form statements, not expressions. For example, the following Gosu is valid:
var i = 1
while (i < 10) {
i++
print(i)
}
The following Gosu is invalid because statements are
impermissible in an expression, which Gosu requires in a while statement:
var i = 1
while (i++ < 10) { // Compilation error!
print(i)
}
Gosu supports the increment and decrement operator
only after a variable, not before a variable, so i++ is valid but ++i is invalid. The ++i form exists in other languages
to support expressions in which the result is an expression that you
pass to another statement or expression. In Gosu, these operators do
not form an expression, so you cannot use increment or decrement in while declarations, if declarations, and for declarations.
See also
