
Available under Creative Commons-ShareAlike 4.0 International License.
Let's look at an example: 2 + 3 * 4 + 5 is our expression but what does it equal?
- the symbols of + meaning addition and * meaning multiplication are our operators
- the values 2, 3, 4 and 5 are our operands
- precedence says that multiplication is higher than addition
- thus, we evaluate the 3 * 4 to get 12
- nowwehave:2+12+5
- the associativity rules say that addition goes left to right, thus we evaluate the 2 +12 to get 14
- nowwehave:14+5
- fnally, we evaluate the 14 + 5 to get 19; which is the value of the expression
Parentheses would change the outcome. (2 + 3) * (4 + 5) evaluates to 45.
Parentheses would change the outcome. (2 + 3) * 4 + 5 evaluates to 25.
- 瀏覽次數:1506