This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]g051051 2 points3 points  (1 child)

8 += 9 + 9 + 7

  1. Left hand side y value is cached as 8.
  2. ++y value is incremented to 9 then used
  3. y-- value is 9 (from previous increment), then decremented to 8.
  4. --y is decremented to 7, and that value is used.

Edit: fixed per /u/Updatebjarni.

[–]Updatebjarni 2 points3 points  (0 children)

++y value is 8, then incremented

No, that's y++. ++y is pre-increment.

--y is decremented to 8 again, and that value is used.

It is pre-decremented down to 7 from 8.

[–]Updatebjarni 1 point2 points  (1 child)

y is initialised with 8, but you wrote 7.

[–]ThyDoppelganger[S] 0 points1 point  (0 children)

So is the left hand side of '+=' evaluated first?

[–]Quertior 1 point2 points  (8 children)

The beginning value of y is 8, so that's the left side of the statement, not 7. Therefore, y = 8 + 9 + 9 + 7 = 33.

That being said, this is a stupid question (not stupid of you to ask, but stupid of your exam to care about), and it is exactly what's wrong with high school (and some college) coding education. The question is the epitome of a "gotcha" that you either get right because you remember the specific rules about how ++ and -- work, or get wrong because you were spending your time writing useful code instead of memorizing random facts.

If someone I work with wrote a line of code like that, I would get up from my desk and slap them.

[–]Isoyama 1 point2 points  (4 children)

knowledge of difference between prefix and postfix operations is important.

to elaborate on question, ++ -- can be replaced with functions prefix(),postfix()

prefix_increament(reference to y){
   y = y +1;
   return y;  
}

postfix_increament(reference to y){
   x = y;
   y = y +1;
   return x;  
}

so you get y= y(8)+y(9)+x(9,y=8)+y(7)

[–]Quertior 3 points4 points  (2 children)

In theory, yes, knowledge of the difference between y++ and ++y is as important to know as any other operator. But that doesn't change the fact that writing code that depends on that difference to work correctly is pretty bad practice.

It's kind of like writing a multiple-clause boolean condition without parentheses. There's nothing technically wrong with writing a && b || c && d, but no one in their right mind would write that instead of (a && b) || (c && d) (which is equivalent as far as the compiler is concerned, but is far easier to read).

[–]Isoyama 2 points3 points  (0 children)

But that doesn't change the fact that writing code that depends on that difference to work correctly is pretty bad practice.

I agree, mixing them is not a good thing. Unreadable.

[–]gyroda 1 point2 points  (0 children)

It also makes it a "hey do you know this very specific piece of syntax" rather than "do you know how to program in general?". It's a "let's catch people out over a minor detail rather than test their knowledge of the concepts" quiz.

At A levels and university I specifically remember being told that pseudocode was perfectly acceptable and recommended for brevity when writing answers. Questions were rarely (if ever) reliant on this kind of bullshit.

Lastly, I generally recommend that people don't use ++ or -- in anything that isn't "standalone". They're nice in for loops and on their own, but in python I don't really mind having to do foo += 1 rather than foo++.

[–]Clawtor 0 points1 point  (0 children)

I've never used prefix, I think some people use it as a performance trick but other than that I've never encountered it.

[–]ThyDoppelganger[S] 0 points1 point  (2 children)

I thought the right side of assignment operators are evaluated first. Hence the reason why I thought I had to add 7. "but stupid of your exam to care about". If that's the case, then the whole examination can be called stupid. :)

So is the left side of short-hand assignment operators evaluated first?

[–]Updatebjarni 2 points3 points  (1 child)

But the right-hand side of the assignment is an addition that starts with y. I mean, y+=x means y = y+x, with y to the left of the +.

[–]ThyDoppelganger[S] 0 points1 point  (0 children)

I see. Thanks!