Prev | Current Page 172 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

..
else
if ( myVar == 2 )
// do this
else
if ( myVar == 3 )
// repeat as necessary
...
else
// if all else fails, do this
if statements used in this way can nest as deep as you like, though it is not necessary to indent them in
this fashion. The majority of developers will often provide the nesting if keyword directly after the
else keyword, providing a seemingly new statement type:
if ( myVar == 1 )
// do this...
else if ( myVar == 2 )
// do this...
else if ( myVar == 3 )
// repeat as necessary
...
else
// if all else fails, do this
Chapter 4: Controlling the Flow of Information
77
As you can see, this will make your code look a lot neater and easier to read. The final else block used
at the end of the nested if statements forms the if all else fails safety net, meaning that any
comparison not yet met will be processed here.
Aligning if statements can be increased further still when using short lines of code, though this is at
your discretion. For example:
if ( 3 == 3 )
myVar++;
else
myVar--;
Can be more readable as:
if ( 3 == 3 ) myVar++ else myVar--;
If you look at the last example, you might notice that the expression preceding the else keyword is not
immediately proceeded by a semicolon (;) ending the line.


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