User Guide

90 Chapter 3: Using Best Practices
Writing conditional statements
Place conditions put on separate lines in if, else-if, and if-else statements. Your if
statements should use braces (
{}). You should format braces like the following examples. The if,
if-else, and else-if statements have the following formats:
//if statement
if (condition) {
//statements;
}
//if-else statement
if (condition) {
//statements;
} else {
//statements;
}
//else-if statement
if (condition) {
//statements;
} else if (condition) {
//statements;
} else {
//statements;
}
You can write a conditional statement that returns a Boolean value, as the following example
shows:
if (cart_array.length>0) {
return true;
} else {
return false;
}
However, compared with the previous code, the ActionScript in the following example is
preferable:
return (cart_array.length > 0);
The second snippet is shorter and has fewer expressions to evaluate. It’s easier to read and
understand.
The following condition checks if the variable
y is greater than zero, and returns the result of x/y
or a value of 0:
if (y>0) {
return x/y;
} else {
return 0;
}
The following example shows another way to write this code:
return ((y > 0) ? x/y : 0);