28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 3 ■ A <strong>Java</strong> 8 Primer: An Introduction to <strong>Java</strong> 8 Concepts and Principles<br />

Table 3-5. <strong>Java</strong> Assignment Operators, What Each Assignment Is Equal to in Code, and a Description of the Operator<br />

Operator Example Description<br />

= C=A+B Basic assignment operator: Assigns value from right-hand operand to<br />

left-hand operand<br />

+= C+=A equals C=C+A ADD assignment operator: Adds right-hand operand to left-hand<br />

operand; puts result in left-hand operand<br />

-= C-=A equals C=C-A SUB assignment operator: Subtracts right-hand operand from left-hand<br />

operand; puts result in left-hand operand<br />

*= C*=A equals C=C*A MULT assignment operator: Multiplies right-hand operand and left-hand<br />

operand; puts result in left-hand operand<br />

/= C/=A equals C=C/A DIV assignment operator: Divides left-hand operand by right-hand<br />

operand; puts result in left-hand operand<br />

%= C%=A equals C=C%A MOD assignment operator: Divides left-hand operand by right-hand<br />

operand; puts remainder in left-hand operand<br />

Finally, you are going to take a look at conditional operators, which also allow you to code powerful game logic.<br />

<strong>Java</strong> Conditional Operators<br />

The <strong>Java</strong> language also has a conditional operator that can evaluate a condition and make a variable assignment<br />

for you, based on the resolution of that condition, using only one compact programming construct. The generic <strong>Java</strong><br />

programming statement for a conditional operator always uses the following basic format:<br />

Variable = (evaluated expression) ? Set this value if TRUE : Set this value if FALSE ;<br />

So, on the left-hand side of the equals sign, you have the variable, which is going to change (be set), based on<br />

what is on the right-hand side of the equals sign. This conforms to what you have learned thus far.<br />

On the right-hand side of the equals sign, you have an evaluated expression, for instance, “x is equal to 3,”<br />

followed by a question mark and then two numeric values that are separated from each other, using the colon, and,<br />

finally, a semicolon to terminate the conditional operator statement. If you wanted to set a variable y to a value of 25<br />

if x is equal to 3, and to 10 if x is not equal to 3, you would write that conditional operator programming statement by<br />

using the following <strong>Java</strong> programming logic:<br />

y = (x == 3) ? 25 : 10 ;<br />

Next, you are going to look at <strong>Java</strong> logic control structures that leverage the operators you just learned about.<br />

<strong>Java</strong> Conditional Control: Decision Making or Loops<br />

As you have just seen, many of the <strong>Java</strong> operators can have a fairly complex structure and provide a lot of processing<br />

power, using very few characters of <strong>Java</strong> programming logic. <strong>Java</strong> also has several more complicated conditional<br />

control structures, which can automatically make decisions or perform repetitive tasks for you, once you have set<br />

up the conditions for those decisions or task repetitions by coding the <strong>Java</strong> logic control structure.<br />

www.it-ebooks.info<br />

65

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!