Game Development Reference
In-Depth Information
keyword to provide Java statement blocks for each outcome for this expression's evalu-
ation. If none of the cases inside a switch statement structure (curly braces) are called
(used) by the expression evaluation, you can also supply a Java default keyword and
Java statement code block for what you want done.
The variable used in the case statements can be one of four Java data types: char
(character), byte , short , or int (integer). You will generally want to add a Java break
keyword at the end of each of your case statement code blocks, at least in the use case,
in which the values being switched between need to be exclusive, and only one is vi-
able (or permissible) for each invocation of the switch statement. The default state-
ment, which is the “if any of these do not match” is the last of the statements inside of
the switch, and does not need this break keyword.
If you do not furnish a Java break keyword in each of your case logic blocks, more
than one case statement can be evaluated in the same pass through your switch state-
ment. This would be done as your expression evaluation tree progresses from top (first
case code block) to bottom (last case code block or default keyword code block). So if
you had a collection of Boolean “flags” such as hasValue, isAlive, isFixed, and so on,
these could all be processed on one single “pass” by using a switch-case statement
structure that does not use any break statements at all.
The significance of this is that you can create some fairly complex decision trees,
based on case statement evaluation order, and whether you put this break keyword at
the end of any given case statement's code block.
The general format for your switch-case decision tree programming construct
would look like this:
switch (expression) {
case value1 :
programming statement one;
programming statement two;
break;
case value2 :
programming statement one;
programming statement two;
break;
default :
programming statement one;
programming statement two;
}
Search WWH ::




Custom Search