简体   繁体   中英

What is the difference between pre-increment and post-increment in the cycle (for/while)?

My interest is in the difference between for and while loops. I know that the post-increment value is used and then incremented and the operation returns a constant pre-increment.

while (true) {
    //...
    i++;
    int j = i;
}

Here, will j contain the old i or the post-incremented i at the end of the loop?

Since the statement i++ ends at the ; in your example, it makes no difference whether you use pre- or post-increment.

The difference arises when you utilize the result:

int j = i++; // i will contain i_old + 1, j will contain the i_old.

Vs:

int j = ++i; // i and j will both contain i_old + 1.

Depends on how you use them.

  • i++ makes a copy, increases i, and returns the copy (old value).
  • ++i increases i, and returns i.

In your example it is all about speed. ++i will be the faster than i++ since it doesn't make a copy.

However a compiler will probably optimize it away since you are not storing the returned value from the increment operator in your example, but this is only possible for fundamental types like a int .

Basic answer for understanding. The incrementation operator works like this:

// ++i
function pre_increment(i) {
    i += 1;
    return i;
}
// i++
function post_increment(i) {
    copy = i;
    i += 1;
    return copy;
}

A good compiler will automatically replace i++ with ++i when it detect that the returned value will not be used.

In pre-increment the initial value is first incremented and then used inside the expression.

a = ++i;

In this example suppose the value of variable i is 5. Then value of variable a will be 6 because the value of i gets modified before using it in a expression.

In post-increment value is first used in a expression and then incremented.

a = i++;

In this example suppose the value of variable i is 5. Then value of variable a will be 5 because value of i gets incremented only after assigning the value 5 to a .

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char **argp)
{
    int x = 5;

    printf("x=%d\n", ++x);
    printf("x=%d\n", x++);
    printf("x=%d\n", x);

    return EXIT_SUCCESS;
}

Program Output:

x=6
x=6
x=7

In the first printf statement x is incremented before being passed to printf so the value 6 is output, in the second x is passed to printf (so 6 is output) and then incremented and the 3rd printf statement just shows that post increment following the previous statement by outputting x again which now has the value 7.

i++ 使用 i 的值然后递增它,但 ++i 在使用它之前递增 i 的值。

The difference between post- and pre-increment is really, in many cases subtle. post incremenet, aka num++ , first creates a copy of num , returns it, and after that , increments it. Pre-increment, on the other hand, aka ++num , first evaluates, then returns the value. Most modern compilers, when seeing this in a loop, will generally optimize, mostly when post increment is used, and the returned initial value is not used. The most major difference between the 2 increments, where it is really common to make subtle bugs, is when declaring variables, with incremented values: Example below:

int num = 5;
int num2 = ++num; //Here, first num is incremented, 
                  //then made 6, and that value is stored in num2;

Another example:

int num = 5;
int num2 = num++; //Here, num is first returned, (unfortunately?), and then 
                  //incremented. This is useful for some cases.

The last thing here I want to say is BE CAREFUL WITH INCREMENTS . When declaring variables, make sure you use the right increment, or just write the whole thing out ( num2 = num + 1 , which doesn't always work, and is the equivalent of pre-increment). A lot of trouble will be saved, if you use the right increment.

it does not matter if you use pre or post increment in an independent statement, except for the pre-increment the effect is immediate

//an example will make it more clear:


int n=1;
printf("%d",n);
printf("%d",++n);// try changing it to n++(you'll get to know what's going on)

n++;
printf("%d",n);

output: 123

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM