Variable assignment
Gosu uses the standard programming assignment operator = to assign the value on the right side of
the statement to the item on the left side of the statement.
Syntax
variable = expressionExamples
count = 0
time = DateUtil.currentDate()
Compound assignment operators
Gosu supports compound assignment operators that perform an action and assign a value in a single statement.
The following table lists each compound operator and describes its behavior. The examples assume that the
variable i is previously declared as type int.
|
Operator |
Description |
Examples |
|---|---|---|
|
= |
Simple assignment of the value on the right side of the operator to the variable on the left side. |
Assigns the value 10. |
|
+= |
Adds the value of the variable on the left side of the operator to the value on the right side. Next, Gosu assigns this result to the variable. |
Assigns the value 13. |
|
-= |
Subtracts the value of the variable on the left side of the operator from the value on the right side. Next, Gosu assigns this result to the variable. |
Assigns the value 7. |
|
*= |
Multiplies the value of the variable on the left side of the operator by the value on the right side. Next, Gosu assigns this result to the variable. |
Assigns the value 30. |
|
/= |
Divides the value of the variable on the left side of the operator by the value on the right side. Next, Gosu assigns this result to the variable. |
Assigns the value 3. For the |
|
%= |
Divides the value of the variable on the left side of the operator by the value on the right side, and returns the remainder. Next, Gosu assigns this result to the variable. |
Assigns the value 1. This value is 10 |
|
||= |
Performs a logical Gosu accepts the primitive type |
Assigns the value |
|
&&= |
Performs a logical Gosu accepts the primitive type |
Assigns the value |
|
&= |
Performs a bitwise |
Assigns the value 10. The decimal number 10 is 1010 binary. The decimal number 15 is 1111 binary. This code does a bitwise AND between value 1010 and 1111. The result is binary 1010, which is decimal 10. Contrast with this example:
Assigns the value 8. The decimal number 10 is 1010 binary. The decimal number 13 is 1101 binary. This does a bitwise AND between value 1010 and 1101. The result is binary 1000, which is decimal 8. |
|
^= |
Performs a bitwise exclusive |
Assigns the value 5. The decimal number 10 is 1010 binary. The decimal number 15 is 1111 binary. This code does a bitwise exclusive OR with value 1010 and 1111. The result is binary 0101, which is decimal 5. Contrast with this example:
Assigns the value 7. The decimal number 10 is 1010 binary. The decimal number 13 is 1101 binary. This does a bitwise AND between value 1010 and 1101. The result is binary 0111, which is decimal 7. |
|
|= |
Performs a bitwise inclusive |
Assigns the value 15. The decimal number 10 is 1010 binary. The decimal number 15 is 1111 binary. This code does a bitwise inclusive OR with value 1010 and 1111. The result is binary 1111, which is decimal 15. Contrast with this example:
Assigns the value 11. The decimal number 10 is 1010 binary. The decimal number 13 is 1101 binary. This does a bitwise AND between value 1010 and 1101. The result is binary 0111, which is decimal 11. |
|
<<= |
Performs a bitwise left shift on the value of the variable on the left side of the operator. The number of bit shifts is the value on the right side of the operator. Next, Gosu assigns this result to the variable. |
Assigns the value 20. The decimal number 10 is 01010 binary. This code does a bitwise left shift of 01010 one bit to the left. The result is binary 10100, which is decimal 20. Contrast with this example:
Assigns the value 40. The decimal number 10 is 001010 binary. This code does a bitwise left shift of 001010 one bit to the left. The result is binary 101000, which is decimal 40. |
|
>>= |
Performs a bitwise right shift on the value of the variable on the left side of the operator. The number of bit shifts is the value on the right side of the operator. Next, Gosu assigns this result to the variable. Important: For signed values, this operator sets the high-order bit with its previous value
for each shift. This behavior preserves the sign (positive or negative) of the result. For signed
integer values, this behavior is usually appropriate. Contrast this operatorwith the
>>>= operator. |
Assigns the value 5. The decimal number 10 is 1010 binary. This code does a bitwise right shift of 1010 one bit to the right. The result is binary 0101, which is decimal 5. Contrast with this example:
Assigns the value -3. The decimal number -10 is 11111111 11111111 11111111 11110110 binary. This code does a bitwise right shift two bits to the right, filling in the top sign bit with the 1 because the original number was negative. The result is binary 11111111 11111111 11111111 11111101, which is decimal -3. |
|
>>>= |
Performs a bitwise right shift on the value of the variable on the left side of the operator. The number of bit shifts is the value on the right side of the operator. Next, Gosu assigns this result to the variable on the left side. Important: This operator sets the high-order bit for each shift to zero. For unsigned
integer values, this behavior is usually appropriate. Contrast this operator with the
>>= operator. |
Assigns the value 5. The decimal number 10 is 1010 binary. This code does a bitwise right shift of 1010 one bit to the right. The result is binary 0101, which is decimal 5. Contrast with this example:
Assigns the value 1073741821. The negative decimal number -10 is 11111111 11111111 11111111 11110110 binary. This code does a bitwise right shift two bits to the right, with no filling of the top bit. The result is binary 00111111 11111111 11111111 11111101, which is decimal 1073741821. The original was a negative number, but in this operator that bit value is filled with zeros for each shift. |
|
++ unary operator |
Adds one to the value of a variable. This operator is also known as the increment-by-one operator.
The unary |
Assigns the value 11. |
|
-- unary operator |
Subtracts one from the current value of a variable. This operator is also known as the
decrement-by-one operator. The unary |
Assigns the value 9. |
Comparison of compound assignments and expressions
The table above lists a variety of compound assignment operators, such as ++, --, and +=.
These operators form statements rather than expressions.
The following Gosu is valid:
while (i < 10) {
i++
print(i)
}
The following Gosu is invalid because statements are not permissible in an expression, which Gosu requires in a
while statement:
while (i++ < 10) { // Compilation error!
print(i)
}
Gosu supports the increment and decrement operator only after a variable, not before a variable. The operation
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. Thus, you cannot use increment or decrement
in while declarations, if declarations, and for
declarations.
