Return to Snippet

Revision: 40877
at February 9, 2011 07:51 by meekgeek


Updated Code
/*Fumio Nonaka in: Coding|Jump To Comments February 9, 2011
The conditional operator ?: is used to select an alternative value to be assigned. It is basically faster than the if statement.*/


myVariable = (condition) ? valueA : valueB;  

view plaincopy to clipboardprint?
if (condition) {  
    myVariable = valueA;  
} else {  
    myVariable = valueB;  
}  
/*However if the value selected by the conditional operator is to be added with the addition assignment operator +=, its operation will come to be slow.*/


myVariable += (condition) ? valueA : valueB;  
//Using the assignment operator instead makes it a little faster.


myVariable = (condition) ? myVariable + valueA : myVariable + valueB;  
//But in this case, the if statement is better to use.


view plaincopy to clipboardprint?
if (condition) {  
    myVariable += valueA;  
} else {  
    myVariable += valueB;  
}

Revision: 40876
at February 9, 2011 07:49 by meekgeek


Initial Code
Fumio Nonaka in: Coding|Jump To Comments February 9, 2011
The conditional operator ?: is used to select an alternative value to be assigned. It is basically faster than the if statement.


myVariable = (condition) ? valueA : valueB;  

view plaincopy to clipboardprint?
if (condition) {  
    myVariable = valueA;  
} else {  
    myVariable = valueB;  
}  
However if the value selected by the conditional operator is to be added with the addition assignment operator +=, its operation will come to be slow.


myVariable += (condition) ? valueA : valueB;  
Using the assignment operator instead makes it a little faster.


myVariable = (condition) ? myVariable + valueA : myVariable + valueB;  
But in this case, the if statement is better to use.


view plaincopy to clipboardprint?
if (condition) {  
    myVariable += valueA;  
} else {  
    myVariable += valueB;  
}

Initial URL
http://blog.jactionscripters.com/2011/02/09/when-to-use-the-conditional-operator/

Initial Description


Initial Title
When to use the ?: conditional operator

Initial Tags


Initial Language
ActionScript 3