order of execution

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

order of execution

Russ Whaley
Can someone please explain this? I'm guessing I don't understand order of execution.

When perusing >>fibonacciSequence, I get a proper result, but I don't understand why when looking at the code.  

Consider this fragment...

prevTotal := 0.
currTotal := 1.
currTotal := prevTotal + (prevTotal := currTotal).

My understanding *was* that parentheses are executed first.  

(prevTotal := currTotal) - assigns and returns 1

currTotal := prevTotal + (1)

and since prevTotal = 1

currTotal := 1 + (1)

prevTotal = 1.
currTotal = 2.

Yet what appears to be happening is... 

prevTotal = 0

currTotal := 0 + (prevTotal := currTotal)

then the parentheses...

currTotal := 0 + (1)

prevTotal = 1.
currTotal = 1.

Care to school me?

Thanks!
Russ

--
Russ Whaley
[hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: order of execution

Steffen Märcker
Hi,

don't forget to read the expression from left to right:

> currTotal := prevTotal + (prevTotal := currTotal).
             5  1         4            3  2
1. the current value (X) of prevTotal is fetched.
2. the current value (Y) of currTotal is fetched
3. prevTotal is assigned currTotal which is also the value of the
parenthesis
4. X is sent the message + with the argument Y
5. currTotal is assigned the result from 4.

Kind regards,
Steffen

Am .09.2020, 15:05 Uhr, schrieb Russ Whaley <[hidden email]>:

> Can someone please explain this? I'm guessing I don't understand order of
> execution.
>
> When perusing >>fibonacciSequence, I get a proper result, but I don't
> understand why when looking at the code.
>
> Consider this fragment...
>
> prevTotal := 0.
> currTotal := 1.
> currTotal := prevTotal + (prevTotal := currTotal).
>
> My understanding *was* that parentheses are executed first.
>
> (prevTotal := currTotal) - assigns and returns 1
>
>
> currTotal := prevTotal + (1)
>
>
> and since prevTotal = 1
>
> currTotal := 1 + (1)
>
>
> prevTotal = 1.
>
> currTotal = 2.
>
>
> Yet what appears to be happening is...
>
> prevTotal = 0
>
> currTotal := 0 + (prevTotal := currTotal)
>
>
> then the parentheses...
>
> currTotal := 0 + (1)
>
>
> prevTotal = 1.
>
> currTotal = 1.
>
>
> Care to school me?
>
> Thanks!
> Russ