+ 2

# Operator Precedence Ruby evaluates a mathematical expression using an order of operations that is based on operator precedence.

What is the output of this code? 5-2*3+3 Solo learn gave the answer as 2. Following their explanation above it should have been -4 But as you can see it was'nt. And none of the values were in parenthesis If you agree with solo learn explain

3 Answers

+ 2

Given : 5-2*3+3
First multiply: 5- (2*3) +3
we'll get: 5- 6 + 3
Second subtract: (5-6) +3
we'll get: -1+3
Third add: (-1+3)
we'll get: 2
Hence, Answer: 2.
Explanation: Simply follow the precedence rule.
After multiplying (2*3) you MUST return back to the starting digit and do the next operation according to precedence. Which means you have to start from beginning after doing each operation.

+ 1

So first comes the multiplication
5-6+3
then the addition
5-3
then the the subtraction
2
I think you may have added 6 and 3 to 9. However you should be adding -6 to 3 to get -3. I would recommend doing subtraction first to avoid any confusion, as subtraction and addition can be done in any order.

0

Remember that we go from left to right.
So as you read first is multiplication/divide.
We have a multiplication 2*3 = 6
So now we have 5 - 6 + 3
Then is the addition/substraction, but don't forget that we go from left to right.
So 5 - 6 = -1
No we have -1 + 3 = 2
I hope it helped you.