Román
Román

Reputation: 146

What is the meaning of i=j--

this is my first question here, and I'm glad to join the community. I have a test tomorrow, and in one of the examples there is that line

i=0; j=2; z=2
i=j--;

What is the exact operation that is done? Because I know that j-- means j-1 everytime. Thanks! I'm using Dr.Java.

EDIT: It was a duplicate. Should I delete?

Upvotes: -3

Views: 5510

Answers (4)

Gerald Shyti
Gerald Shyti

Reputation: 51

This means that i will have as value 2, because you used a post-decrement operator. J will have the value of 1 since it is decremented. If you had written:

i = --j

then i will have the value of 1, since the value is decremented BEFORE assignment.

Gerald

Upvotes: 0

Enzokie
Enzokie

Reputation: 7415

It is another shortcut for this code.

i=0; j=2; z=2
i = j;
j = j - 1;

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074038

i = j--; is an assignment statement. The right-hand side is evaluated, and the resulting value is assigned to the thing on the left-hand side. In your case, that means:

  1. The value of j is read (2, in that code)
  2. j is decremented (to 1, in that code)
  3. The value read in step 1 (2) is assigned to i

The order of Steps 1 and 2 is because the -- is after the j; it's the postfix decrement operator. If it were before the j (the prefix decrement operator), steps 1 and 2 would be reversed.

Upvotes: 6

Jeremy Kahan
Jeremy Kahan

Reputation: 3826

It means set i equal to j and then subtract one from j.

Upvotes: 4

Related Questions