There is a code ( updated ):

int s = 0; for (int i = 0; i < 10; i++) { s = s + s++; } System.out.println(s); 

Actually the question is why s after the cycle will be equal to 0 ?

    3 answers 3

    Because the assignment s = works after the increase in s ++ works out.

    In steps:

    1. first the value of s is incremented by 1, the value in memory is updated
    2. then s ++ returns the previous value, i.e. 0
    3. the assignment operation is performed and the value s is recalled in memory back to 0

    If you used ++ s, everything would be different. In step 2, ++ s would return 1, as expected. True, the "s = ++ s" construct would be no different from "++ s" or "s ++", except for an extra write operation in memory.

    UPD c taking into account the change in listing, as yozh said, the explanation remains valid

    there is an expression s = s + s ++

    1. the left part of the expression is taken, up to "+", on the left is s .. there is still 0.
    2. the right side of the expression is taken, after the "+" .. there s ++ .. an increment is executed and written to s = 1, but 0 is returned
    3. Adds 0 + 0
    4. Writing to the variable s = 0 is performed, so the result of the increment is overwritten and lost.
    • cy6erGn0m, a bit wrong wrote the listing s = 0; for (int i = 0; i <10; i ++) {s = s + s ++; } System.out.println (s); // here s = 0; (already after working out the cycle) as far as I understand the logic: 1. s = 0; 2. go into the cycle. on the first iteration: s = s + s ++; in fact, we get: s = 0 + 0; 3. s increment 4. condition check and go to next iteration. But apparently something is wrong ( - corri
    • @corri better fix the code in question - yozh
    • The explanation of @ cy6erGn0m is still valid for the new case. The main thing is that s++ returns the old value. But the assignment of a new value to the variable s occurs during the increment itself. - yozh

    The variable s will increase by one only after the execution of the TOTAL block, if it is necessary for it to increase the value BEFORE the execution of the block, you must write ++s .


    I reworked your code a bit, I'm just starting to work with java myself, but I think the problem is that 0 is not incremented (I do not claim it).

     class One { public static void main(String[] args) { int s = 1; for(int i = 0; i<10; i++) { s = s + s++; System.out.println(s); // тут s = 0; (уже после отработки цикла) } } } 

    If you give s a value of 1, then we get

     2 4 8 16 32 64 128 256 512 1024 

    In my opinion, this is what was required. in the first line the value is 2, which means that when outputting to System.out.println (); the increment has not yet occurred, otherwise there would be 3.

    • DroidAlex, s = s + s ++; and s + = s ++; - identical entries + = is written for convenience. The compiler still performs the same actions. arachnoden, no. postincrement s will work after the operation +; those. if the code were int s = 0, k = 0; for (int i = 0; i <10; i ++) {k = s ++; System.out.println (s); // 1} then s would be equal to 1 in line 1 already in the first iteration - corri
    • The increment does not occur after the block (}) closed, but after System.out.println(s); which java and considers the end of the block. - arachnoden
    • No! arachnoden, if there is a desire - check) in System.out.println (s); in the first iteration, s will be equal to 1, and not zero i will naturally be equal to 0 as it should be - corri
     a = ++i // сначала i увеличивается на 1, потом присваивается к а b = i++ // сначала i присваивается к b, потом увеличивается на 1 

    Different compilers may handle these operations in different ways, so it is advised not to abuse such constructs. A good article is on the habr about the construction i = i++ + ++i;

    • one
      What other different compilers? Question about java. And Java has a clear JLS that says exactly what should happen. - cy6erGn0m
    • just paid attention to it, but here it is specifically about Java, but in a different language, you can get other values - Gorets
    • besides, this code: a = ++ i // first i is increased by 1, then assigned to a b = i ++ // first i is assigned to b, then it increases by 1, it works the same way in any compiler that knows about increment / decrement corri