Game Development Reference
In-Depth Information
programming statement two;
}
You can also add an else keyword to make this decision-making structure evaluate
statements that would need to execute if the boolean variable (true or false condition)
evaluates to false rather than true , which makes this structure more powerful (and use-
ful). This general programming construct would then look like this:
if (expression = true ) {
programming statement one;
programming statement two;
} else {
// Execute this code
block if (expression = false )
programming statement one;
programming statement two;
}
In addition, you can nest if-else structures, thereby creating if{}-{else if}-{else if}-
else{} structures. If these structures get nested too deeply, then you would want to
switch (no pun intended) over to the switch-case structure, which will become more
and more efficient, relative to a nested if-case structure, the deeper the if-else nesting
goes. For example, the switch-case statement that you coded earlier for the InvinciBa-
gel game, if translated into a nested if-else decision-making construct, would look like
the following Java programming structure:
if (ibState = ' F ') {
deathLogicFlying();
} else if (ibState = ' J ') {
deathLogicJumping();
} else if (ibState = ' R ') {
deathLogicRunning();
} else {
deathLogicIdle();
}
As you can see, this if-else decision tree structure is quite similar to the switch-case
that you created earlier, except that the decision code structures are nested inside each
other rather than contained in a flat structure. As 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 evaluation
Search WWH ::




Custom Search