Prev | Current Page 173 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

This is to help facilitate the readability of the
whole if statement and is allowed only with single lines that directly follow the conditional expression
of the if statement.
switch Statements
switch statements provide an alternative to if statements that repeatedly compare the equality of a
single value. For example, you may be using an integer flag to store a particular state then perform
different tasks based on that flag. If the value of that flag can be anything from 0 to 255, for example,
then you may require an awful lot of else if statements. The switch statement can help to tidy such
code by making it more readable.
The switch statement is written similarly to an if statement, with the exception that the value to
compare is supplied to the brackets following the switch keyword, while the various values supplied
for the comparison are provided by the case keyword and are listed within the switch ??™ s code block,
like this:
switch ( myValue )
{
case val1:
// do code
case val2:
// do code
.


Pages:
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185