Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 5 ■ a Java primer: introduCtion to Java ConCepts and prinCiples

As you can see, this if-else decision tree structure is quite similar to the switch-case that we created
earlier, except that the decision code structures are nested inside of each other, rather than contained
in a “flat” structure. As a general rule of thumb, I would use an if, and the if-else, for one- and two-value
evaluations, and I would use a switch-case for three-or-more-value evaluation scenarios. I use a switch-case
structure extensively in my books covering Android, such as Android Apps for Absolute Beginners (Apress,
2017) and Pro Android Wearables (Apress, 2015).
Next, let’s take a look at the other types of conditional control structures that are used extensively in
Java, the “looping” or iterative programming structures. These iterative conditional structures will allow you
to execute any block of programming statements a predefined number of times by using the for loop or until
the Java programming objective has been achieved by using either the while or do-while loop.
As you might imagine, these iterative control structures can be extremely useful for your game control
logic.


Looping Control Structures: While, Do - While, and the For Loop


Whereas the decision tree type of control structure is traversed a fixed number of times (once all the way
through, unless a break [switch-case], or resolved expression [if-else], is encountered), a looping control
structure keeps executing over time, which for the while and do-while structures makes them a bit
dangerous as an infinite loop could be generated if you are not careful with your programming logic! The
for loop structure executes for a finite number of loops specified in the definition of the loop, as we will see
during this section of the chapter.
Let’s start with the finite loop and cover the for loop first. A Java for loop uses the following general
format :


for(initialization; boolean expression; update equation) {
programming statement one;
programming statement two;
}


The three parts of the evaluation area for the for loop, inside the parentheses, are separated
by semicolons, and each contains a programming construct. The first is a variable declaration and
initialization, the second is a Boolean expression evaluation, and the third is an update equation showing
how to increment the loop during each pass.
If you wanted to move the GamePiece 40 pixels diagonally on the board, your for loop would be as
follows:


for (int x; x < 40; x = x + 1) { // Note: the x = x + 1 statement could also be coded as x++
gamePieceX++; // Note: gamePieceX++ could be coded gamePieceX = gamePieceX + 1;
gamePieceY++; // Note: gamePieceY++ could be coded gamePieceY = gamePieceY + 1;
}


The while (or do-while) type of loop, on the other hand, does not execute over a finite number of
processing cycles but rather executes the statements inside of the loop until a condition is met, using the
following structure:


while (boolean expression) {
programming statement one;
programming statement two;
expression incrementation;
}

Free download pdf