Why doesn't changing the pre to the post increment at the iteration part of a for loop make a difference?

后端 未结 27 2450
-上瘾入骨i
-上瘾入骨i 2020-11-30 06:22

Why does this

 int x = 2;
    for (int y =2; y>0;y--){
        System.out.println(x + \" \"+ y + \" \");
        x++;
    }

prints the s

相关标签:
27条回答
  • 2020-11-30 06:51

    The check is done before the increment argument is evaluated. The 'increment' operation is done at the end of the loop, even though it's declared at the beginning.

    0 讨论(0)
  • 2020-11-30 06:51

    Because this:

    int x = 2;
    for (int y =2; y>0; y--){
        System.out.println(x + " "+ y + " ");
        x++;
    }
    

    Effectively gets translated by the compiler to this:

    int x = 2;
    int y = 2
    while (y > 0){
        System.out.println(x + " "+ y + " ");
        x++;
        y--;
    }
    

    As you see, using y-- or --y doesn't result in any difference. It would make a difference if you wrote your loop like this, though:

    int x = 2;
    for (int y = 3; --y > 0;){
        System.out.println(x + " "+ y + " ");
        x++;
    }
    

    This would yield the same result as your two variants of the loop, but changing from --y to y-- here would break your program.

    0 讨论(0)
  • 2020-11-30 06:51

    There's many similar posts at Stackoverflow:

    • Difference between i++ and ++i in a loop?
    • Is there any performance difference between ++i and i++ in C#?
    • Is there a performance difference between i++ and ++i in C?

    However, it seems your question is more generic because it's not specific to any language or compiler. Most of the above questions deal with a specific language/compiler.

    Here's a rundown:

    • if we are talking about C/C++/Java (probably C# too) and a modern compiler:
      • if i is an integer (const int, int, etc.):
        • then the compiler will basically replace i++ with ++i, because they are semantically identical and so it doesn't change the output. this can be verified by checking the generated code / bytecode (for Java, I use the jclasslib bytecode viewer).
      • else:
    • else:
      • all bets are off, because the compiler cannot guarantee that they are semantically identical, so it does not try to optimize.

    So if you have a class in C++ that overrides the postfix and prefix operators (like std::iterator), this optimization is rarely, if ever, done.

    In summary:

    • Mean what you say, and say what you mean. For the increment part of for loops, you almost always want the prefix version (i.e., ++i).
    • The compiler switcheroo between ++i and i++ can't always be done, but it'll try to do it for you if it can.
    0 讨论(0)
  • 2020-11-30 06:53

    There is no differences because every part of the for "arguments" are separated statements.

    And an interesting thing is that the compiler can decide to replace simple post-incrementations by pre-incrementations and this won't change a thing to the code.

    0 讨论(0)
  • 2020-11-30 06:53

    There is quite confusion in between post and pre increment operator, this can be easily understand from this excerpt of "Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne"

    Increment/decrement operators: i++ is the same as i = i + 1 and has the value i in an expression. Similarly, i-- is the same as i = i - 1. The code ++i and --i are the same except that the expression value is taken after the increment/ decrement, not before.

    for example

    x = 0; 
    post increment:
    x++;
    step 1: 
    assign the old value (0) value of the x back to x.So, here is x = 0.
    step 2:
    after assigning the old value of the x, increase the value of x by 1. So,
    x = 1 now;
    
    when try to print somthing like:
    System.out.print(x++);
    the result is x : 0. Because only step one is executed which is assigning 
    old value of the x back and then print it.
    
    But when, we do operation like this: 
    i++;
    System.out.print(i);
    the result is x: 1. which is because of executing Step one at first 
    statement and then step two at the second statement before printing  the 
    value.
    
    pre increment:
    ++x;
    step 1: 
    increase the value of x by 1. So, x = 1 now;
    step 2:
    assign the increased value back to x.
    
    when try to print something like:
    System.out.print(++1)  
    the result is x : 1. Because the value of the x is raised by 1 and then 
    printed. So, both steps are performed before print x value. Similarly, 
    executing 
    ++i;
    system.out.print(i);
    Both steps are executed at statement one. At second statement, just the 
    value of "i" is printed.
    
    0 讨论(0)
  • 2020-11-30 06:55

    Because nothing in your examples is using the value returned from the pre- or post-increments. Try wrapping a System.out.println() around the ++x and x++ to see the difference.

    0 讨论(0)
提交回复
热议问题