Orphaned Response

pedz at ctvax pedz at ctvax
Tue Nov 26 08:56:00 AEST 1985


I will probably regret this but here goes.

The sequence of

i = 1;
j = ++i + (++i + ++i);

can not leave j with a value of 12.  In fact, the value can not be
greater than 9 and or less than 6.  My line of reasoning is this:

Suppose we have the statement:

j = (i = 2) + (i = 3);

This will clearly leave j with the value of 5 but i's value may be 2
or 3.  The statement ++i is the same as i += 1 which is the same as i
= i + 1.  (I realize that if i were some expression with side effects,
the first two expressions are suppose to cause the side effect only
once but thats not important with a simple variable like i.)  Thus if
we convert the above statement we have

j = (i = i + 1) + (i = i + 1) + (i = i + 1)

Since we go in with i = 1, at least one of the factors will return a
value of 2.  All of them could return a value of 2.  One or two of
them could return a value of 3, and not more than one of them may
return a value of 4.  Thus the highest value is 9, not 12.

I realize also that this is a stupid point, the example was only done
in fun, and there are many other obvious reasons to flame me but I
just had to say this.

Perry Smith
pedz at ctvax



More information about the Comp.sources.unix mailing list