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 />

}<br />

} else if(ibState = 'R') {<br />

deathLogicRunning();<br />

} else {<br />

deathLogicIdle();<br />

As you can see, this if-else decision tree structure is quite similar to the switch-case that you created earlier,<br />

except that the decision code structures are nested inside each other rather than contained in a flat structure. As<br />

a rule of thumb, I would use the if and if-else for one- and two-value evaluations and a switch-case for three-value<br />

evaluation scenarios and greater. I use this switch-case structure extensively in my books covering Android.<br />

Next, let’s take a look at the other types of conditional control structures that are used extensively in <strong>Java</strong>, the<br />

looping programming structures. These allow you to execute a block of programming statements a predefined<br />

number of times (using the for loop) or until an objective is achieved (using a while or a do-while loop).<br />

Looping Control Structures: While, Do-While, and For<br />

Whereas the decision tree type of control structure is traversed a fixed number of times (once all the way through,<br />

unless a break [switch-case] or resolved expression [if-else] is encountered), looping control structures keep<br />

executing over time, which, with respect to the while and do-while structures, makes them a bit dangerous, as an<br />

infinite loop can be generated, if you are not careful with your programming logic! The for loop structure executes for<br />

a finite number of loops (the number is specified in the definition of the for loop), as you will soon see in this section.<br />

Let’s start with the finite loop, covering the for loop first. A <strong>Java</strong> for loop uses the following general format:<br />

for(initialization; boolean expression; update equation) {<br />

programming statement one;<br />

programming statement two;<br />

}<br />

As you can see, the three parts of the evaluation area of the for loop are inside the parentheses, separated by<br />

semicolons, as each contains a programming statement. The first is a variable declaration and initialization, the<br />

second is a boolean expression evaluation, and the third is an update equation showing how to increment the loop<br />

during each pass.<br />

To move the InvinciBagel 40 pixels diagonally on the screen, along both X and Y, the for loop is as follows:<br />

for(int x=0; x < 40; x = x + 1) { // Note: an x = x + 1 statement could also be coded as x++<br />

invinciBagelX++; // Note: invinciBagelX++ could be coded invinciBagelX = invinciBagelX + 1;<br />

invinciBagelY++; // Note: invinciBagelY++ could be coded invinciBagelY = invinciBagelY + 1;<br />

}<br />

In contrast, the while (or do-while) type of loop does not execute over a finite number of processing cycles, but<br />

rather executes the statements inside the loop until a condition is met, using the following structure:<br />

while(boolean expression) {<br />

programming statement one;<br />

programming statement two;<br />

}<br />

68<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!