pharo-users@lists.pharo.org

Any question about pharo is welcome

View all threads

order of execution

RW
Russ Whaley
Fri, Sep 18, 2020 1:05 PM

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
whaley.russ@gmail.com

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 whaley.russ@gmail.com
SM
Steffen Märcker
Fri, Sep 18, 2020 4:45 PM

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 whaley.russ@gmail.com:

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

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 <whaley.russ@gmail.com>: > 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