Code Blocks


In the previous sections, we have been talking about conditionals and their related code blocks. Each time I followed the conditional statement with curly braces. That's not the only way to do it, though. If you have a code block that is only one line long, you can omit the curly braces.

At first this makes it hard to read, but if you use proper indentation, it's not. Consider the following example:

 if(20 > 10)     trace("20 is greater than 10"); //line 2 trace("this line is not part of the conditional block"); //line 3 

As you can see by my indentation, the third line does not occur inside the block of code following the if statement. Instead, I have omitted the curly braces, causing Flash to assume there is only 1 line of code with the if statement: line 2.

You will see me code both ways. I use the one-line version when I am pretty sure there will only be one line in the block. I also use the one-line version with the curly braces, even if there is only one line, if I expect to add more to the block later. It's just one of the things you learn to see as you become a better programmer.

Tip  

It never hurts to add the braces; their omission is a luxury. If you are in doubt or you feel the lack of braces is less readable, feel free to add them any time.




Macromedia Flash MX 2004 Game Programming
Macromedia Flash MX 2004 Game Programming (Premier Press Game Development)
ISBN: 1592000363
EAN: 2147483647
Year: 2004
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net