Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

Chapter 5 ■ a Java primer: introduCtion to Java ConCepts and prinCiples


Java Conditional Operator: Set One Value If True, Another If False


The Java language also has a conditional operator that can evaluate a condition and make a variable
assignment based upon the resolution of that condition for you, using only one compact programming
construct. The generic Java programming statement format for a conditional operator always takes the
following basic format:


Variable = (evaluated expression)? Set this value if TRUE : Set this value if FALSE ;


So, on the left side of the equals sign, you have the variable that is going to change (going to be set)
based on what is on the right side of the equals sign, which conforms to what you have learned during this
section thus far.
On the right side of the equals sign, you have an evaluated expression. For instance, “x is equal to
three,” and then you have the question mark character. After that you have two numeric values that are
separated from each other using the colon character, and finally, the conditional operator statement is
terminated using the semicolon. If you wanted to set a variable y to the value of 25 if x was equal to 3 and
otherwise set its value to 10 if x was not equal to 3, you would write that conditional operator programming
statement by using the following Java programming logic:


y = (x == 3)? 25 : 10 ;


It is important to note that the data types of the expression after the? and after the : must agree with
the type of data variable on the other side of the equals operator. As an example, you cannot specify the
following:


int x = (y > z)? "abc" : 20;


Next we’re going to look at Java logic control structures that leverage the operators you just learned
about.


Java Conditional Control: Loops or Decision Making


As you have just seen, many of the Java operators, especially the conditional operator, can have a fairly
complex program logic structure and provide a ton of processing power using very few characters of Java
programming code. Java also has several more complicated conditional control structures, which can make
decisions automatically for you or automatically perform repetitive tasks for you once you have set up the
conditions for Java to make those decisions. You can also carry out those task repetitions by coding what is
popularly called a Java logic control structure.
In this section of the chapter, we will first take a look at decision-making control structures, such as the
Java Switch-Case structure and the If-Then-Else structure, and then we will take a look at Java’s looping
control structures, including the For, While, and Do-While iterative (looping) control structures.


Decision-Making Control Structures: Switch - Case and If - Else


Some of the most powerful Java logic control structures, especially when it comes to pro Java games
development, are those that allow you to define gameplay decisions that you want your gameplay program
logic to make for you as your game application is running. One of these, called a switch, provides a case-by-
case “flat” decision matrix, and the other, called an if-else, provides a cascading decision tree, evaluating “if
this, do this, if not, else do this, if not, else do this, if none of these, else do this.” Both of these can be used to

Free download pdf