## Forum Stats

• 3,874,662 Users
• 2,266,761 Discussions

Discussions

# Execution order of the equation

Member Posts: 167
edited Mar 19, 2013 5:04AM
Hey Experts,

In PL/SQL block I am having below equation
`Amount := Amount1- Amount2-Amount3`
what will be excution order , Amount1- Amount2 will get excuted first or Amount2-Amount3 ?

Tagged:
«1

• Member Posts: 3,402 Gold Trophy
SShubhangi wrote:
Hey Experts,

In PL/SQL block I am having below equation
`Amount := Amount1- Amount2-Amount3`
what will be excution order , Amount1- Amount2 will get excuted first or Amount2-Amount3 ?

Have you tried it? What were your results? It probably only takes 1 minute to set up a test case.
• Member Posts: 1,453
edited Mar 19, 2013 4:38AM
Its Left to Right
Paul it was typo .

Edited by: Rahul India on Mar 19, 2013 2:06 PM
• Member Posts: 2,319 Silver Trophy
SShubhangi wrote:
Hey Experts,

In PL/SQL block I am having below equation
`Amount := Amount1- Amount2-Amount3`
what will be excution order , Amount1- Amount2 will get excuted first or Amount2-Amount3 ?
Sorry, I must be missing something here, but I do not recollect the last time I saw a difference of results due to execution order for arithmatics involving Addition and Subtraction.

Assuming Amount1 = 7, Amount2 = 6, Amount3 = 5;
Amount1- Amount2 = 7 - 6 = 1 -Amount3(5) = -4
Amount1 + (-1 * (Amount2 + Amount3)) = 7 - (6 + 5) = -4

Is this not correct?
• Member Posts: 64
>
Its Right to Left
>

Ouch. That's just plain wrong. It's left to right, unless you set brackets. That's not even a PL/SQL questions, it's pure maths.

@Purvesh, you're right, but I think OP considered the possibility of
`amount1 - (amount2 - amount3)`
which is not the same as the posted equation.
• Member Posts: 3,402 Gold Trophy
Rahul India wrote:
Its Right to Left
Once again you come up with a lovely wrong answer.

For example 7 - 6 - 1

Left to right

(7-6) - 1 = 0

Right to left

7 - (6-1) = 2
```SQL> select 7 - 6 -1 from dual;

7-6-1
----------
0```
• Member Posts: 2,908 Gold Trophy
edited Mar 19, 2013 4:44AM
BODMAS -- (Bracket Order Division Multiplication Addition Subtraction from left to right in PLAIN MATHEMATICS--( well paranthesis can override this ))

http://en.wikipedia.org/wiki/Order_of_operations

and in oracle :

http://docs.oracle.com/cd/E17952_01/refman-5.6-en/operator-precedence.html

----------------------------------
INTERVAL
BINARY, COLLATE
!
- (unary minus), ~ (unary bit inversion)
^
*, /, DIV, %, MOD
-, +
<<, >>
&
|
= (comparison), <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP, IN
BETWEEN, CASE, WHEN, THEN, ELSE
NOT
&&, AND
XOR
||, OR
= (assignment), :=
--------------------------------------------

As you are dealing with only subtraction, its from left to right.

Cheers,
Manik.

Edited by: Manik on Mar 19, 2013 2:11 PM

Edited by: Manik on Mar 19, 2013 2:14 PM
• Member Posts: 3,402 Gold Trophy
Purvesh K wrote:
SShubhangi wrote:
Hey Experts,

In PL/SQL block I am having below equation
`Amount := Amount1- Amount2-Amount3`
what will be excution order , Amount1- Amount2 will get excuted first or Amount2-Amount3 ?
Sorry, I must be missing something here, but I do not recollect the last time I saw a difference of results due to execution order for arithmatics involving Addition and Subtraction.

Assuming Amount1 = 7, Amount2 = 6, Amount3 = 5;
Amount1- Amount2 = 7 - 6 = 1 -Amount3(5) = -4
Amount1 + (-1 * (Amount2 + Amount3)) = 7 - (6 + 5) = -4

Is this not correct?
No, addition and multiplication are associative (which means it doesn't matter what order you do them in)

(5+2)+3=5+(2+3)
(5*2)*3=5*(2*3)

for example.

Subtraction and division are not associative.

(7-6)-1 != 7-(6-1)
(3/5)/6 != 3/(5/6)
• Member Posts: 8
`Amount := Amount1- Amount2-Amount3`
Amount2 - amount3 is executed first.

Regards,

• Member Posts: 2,319 Silver Trophy
Christine Schnittker wrote:
>
Its Right to Left
>

Ouch. That's just plain wrong. It's left to right, unless you set brackets. That's not even a PL/SQL questions, it's pure maths.

@Purvesh, you're right, but I think OP considered the possibility of
`amount1 - (amount2 - amount3)`
which is not the same as the posted equation.
Perhaps, OP got confused in the Right to Left arithmatics will involve splitting of Expressions and reversals of Signage. And yes, I do agree that this is plain Maths than PL/SQL.
• Member Posts: 3,402 Gold Trophy
903909 wrote:
`Amount := Amount1- Amount2-Amount3`
Amount2 - amount3 is executed first.

Regards,