Game Development Reference
In-Depth Information
Operator
Operation
Description
Decrement
--
Subtract 1
Decreases the value of the operand by 1
To implement the arithmetic operators, place the data field (variable) that you want
to receive the results of the arithmetic operation on the
left side
of the
equals assign-
ment operator
and the variables that you want to perform arithmetic operations on the
right side of the equals sign. Here is an example of
adding
an x and a
y
variable and
assigning the result to a z variable:
Z = X
+
Y;
// Using an Addition Operator
If you want to
subtract
y from x, you use a
minus
sign rather than a plus sign; if
you want to
multiply
the x and y values, you use an
asterisk
rather than a plus sign;
and if you want to
divide
x by y, you use a
forward slash
instead of a plus sign. Here
is how those operations look:
Z = X
-
Y; // Subtraction Operator
Z = X
*
Y; // Multiplication Operator
Z = X
/
Y; //
Division Operator
You will be using these arithmetic operators quite a bit, so you will get some great
practice with these before you are finished with this topic! Let's take a closer look at
relational operators next, as sometimes you will want to compare values rather than
calculate them.
Java Relational Operators
The Java
relational operators
are used to make
logical comparisons
between two
variables or between a variable and a constant, in some circumstances. These should be
familiar to you from school and include equals
,
not equal
,
greater than
,
less than
,
great-
er than or equal to
, and
less than or equal to. In Java, equal to uses two equals signs
side by side between the data fields being compared and an exclamation point before
an equals sign to denote “
not equal to
.”
Table 3-3
shows the relational operators, along
with an example and a description of each.
Description of the Relational Operation