You could also opt for a different route and add the 2 and the 1 to make
3, then multiply by 10 to get 30. With operator precedence, however, the order states that the * operator
will be executed before the + operator. Therefore, the result should be 21. Likewise, if you examine the
next equation, what would you expect the result to be?
var result:Float = 21 * 2 + 5 - 4 / 2;
The order of precedence dictates that the first part of the equation that will be calculated is 21 * 2.
Next, the equation 4 / 2 is calculated. The answer to the first equation is then added to 5, which will
equate to 47 and the second equation is then subtracted from this value, making 45. As you can see, even
when you know the order of precedence of operators, it can still be difficult to immediately see how the
equation should be tackled.
Chapter 3: Learning the Basics
57
To aid you in producing formulas that are easier to discern, you could use the grouping operators. As the
grouping operators are always the first operators to be evaluated, this can enable you to force an order of
calculation while providing a more pleasing way to present your data.
Pages:
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146