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

8

20

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?

Micrometer answered 28/6, 2013 at 14:19 Comment(5)
What do you mean by cycle?Sim
I don't understand your question. Can you post some sample code?Wilburn
possible duplicate of Is there a performance difference between i++ and ++i in C++?Tankoos
in a standalone statement, if i is a primitive type, there is no difference between these two.Massy
banarun --- is it not clear ? Look in the header issue. Тhere is written for/while !!!Micrometer
K
49

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.
Kurzawa answered 28/6, 2013 at 14:28 Comment(5)
int j = ++i; // i will be incremented, and j will contain i+1. This is false. j will still contain i, but it is already incremented so there is no need to make it contain i + 1.Selfinductance
@Selfinductance "j will contain i+1" refers to the original value of i - admittedly not an ideal formulation. j and i will both have the same value when the statement completes.Kurzawa
An important exception is when ++i is mixed w/ other i use in complex statments. printXY(++i, i++) and a[i] = ++i produce pathological results when tested across multiple compilers. The solution is to avoid multiple usage of variables preincremented, or dependent on preincremented values. For instance a[i] = i; can be rewritten as i++; a[i] = i; for consistency and clarity. See: #2990204 For more info.Gyp
@JasonR.Mick Absolutely, though I think you have a typo in that second-to-last code snippet (should probably be a[i] = ++i). Note though that the actual answer to the question as asked is the first sentence. The rest was just to add a bit more context, and could obviously be expanded.Kurzawa
@Kurzawa Good catch, indeed should have read: ... a[i] = ++i; can be rewritten as i++; a[i] = i; .... (second copy of a[i] = ++i; in the above comment is missing the pre-increment operator).Gyp
S
45

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.

Selfinductance answered 28/6, 2013 at 14:29 Comment(10)
Assuming i isn't of class type, it makes no difference in speed. Even for a class type, pre-increment isn't necessarily faster than post-increment, depending on the implementation.Kurzawa
@Kurzawa I hope you mean it the other way around. And even for class types it might be faster to use pre-increment. Like iterators.Selfinductance
Generally, pre-increment will be the same or faster than post-increment (since it shouldn't need to make a copy). Depending on the implementation, it could also be the other way around (though this would probably be a pretty poor implementation).Kurzawa
Even iterators generally have identical performance between pre- and post-increment, since they are usually just wrapped pointers. Still, you are right that using pre-increment when possible might gain you some performance, and almost certainly won't be worse.Kurzawa
@Kurzawa I don't think I understand what you're first comment is supposed to mean, while you're confirming my answer in your second.Selfinductance
@Kurzawa About the iterators, an iterator depends heavily on its containers implementation. Calling it a wrapped pointer is somewhat vague in my opinion. Besides pre-increment still returns an reference instead of an copy in the case of iterators.Selfinductance
I was just commenting on the fact that there is not necessarily a difference in speed - neither for builtin types nor for class types. As for iterators, even though the implementation of the increment, decrement and other operators depends heavily on the container, pretty much all iterators have a sizeof equal to that of a pointer (std::vector<bool>::iterator is one rare exception).Kurzawa
@Kurzawa See the linked question for disapproval for your statements above. If it is being optimized, I think that's where you're after, then it is nothing more or less then calling a pre-increment operator, if a class follows the usual design. Besides iterators are not always holding a single pointer, take a look at a linked list iterator.Selfinductance
let us continue this discussion in chatKurzawa
The brevity of this answer shouldn't be a discount on its validity. The first statement is exactly what happens, and a surprisingly large number of engineer are oblivious to that fact. Given j = i++; they assume the increment always takes place after the assignment, when in reality it is pinned to the eval of i ; the assignment is literally an aftert-thing.Barela
F
24

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.

Furnivall answered 10/12, 2015 at 9:30 Comment(0)
G
6

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 .

Grad answered 28/6, 2013 at 14:58 Comment(0)
G
5
#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.

Godard answered 13/12, 2014 at 16:18 Comment(0)
S
2

i++ uses i's value then increments it but ++i increments i's value before using it.

Storage answered 27/3, 2018 at 23:0 Comment(0)
L
0

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.

Lymphoblast answered 5/8, 2016 at 17:49 Comment(0)
M
0

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

Marvamarve answered 9/11, 2018 at 22:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.