Questions tagged [post-increment]

For issues relating to defining or performing post increment operations.

Post-increment operators increase the value of their operand by 1 (or another specified amount), but the value of the operand in the expression is the operand's original value prior to the increment operation.

561 questions
1146
votes
20 answers

What is the difference between ++i and i++?

In C, what is the difference between using ++i and i++, and which should be used in the incrementation block of a for loop?
The.Anti.9
  • 43,474
  • 48
  • 123
  • 161
531
votes
14 answers

Is there a performance difference between i++ and ++i in C?

Is there a performance difference between i++ and ++i if the resulting value is not used?
Mark Harrison
  • 297,451
  • 125
  • 333
  • 465
415
votes
20 answers

Is there a performance difference between i++ and ++i in C++?

We have the question is there a performance difference between i++ and ++i in C? What's the answer for C++?
Mark Harrison
  • 297,451
  • 125
  • 333
  • 465
345
votes
22 answers

Difference between pre-increment and post-increment in a loop?

Is there a difference in ++i and i++ in a for loop? Is it simply a syntax thing?
GurdeepS
  • 65,107
  • 109
  • 251
  • 387
307
votes
18 answers

What is x after "x = x++"?

What happens (behind the curtains) when this is executed? int x = 7; x = x++; That is, when a variable is post incremented and assigned to itself in one statement? I compiled and executed this. x is still 7 even after the entire statement. In my…
Michael
  • 3,935
  • 4
  • 24
  • 25
138
votes
14 answers

How do the post increment (i++) and pre increment (++i) operators work in Java?

Can you explain to me the output of this Java code? int a=5,i; i=++a + ++a + a++; i=a++ + ++a + ++a; a=++a + ++a + a++; System.out.println(a); System.out.println(i); The output is 20 in both cases
Ankit Sachan
  • 7,690
  • 16
  • 63
  • 98
124
votes
5 answers

Difference between *ptr += 1 and *ptr++ in C

I just started to study C, and when doing one example about passing pointer to pointer as a function's parameter, I found a problem. This is my sample code : #include #include #include int* allocateIntArray(int*…
huy nguyen
  • 1,381
  • 2
  • 11
  • 18
121
votes
12 answers

Incrementing in C++ - When to use x++ or ++x?

I'm currently learning C++ and I've learned about the incrementation a while ago. I know that you can use "++x" to make the incrementation before and "x++" to do it after. Still, I really don't know when to use either of the two... I've never really…
Jesse Emond
  • 7,180
  • 7
  • 32
  • 37
116
votes
8 answers

The difference between ++Var and Var++

In programming, particularly in Java, what is the difference between: int var = 0; var++; and int var = 0; ++var; What repercussions would this have on a for loop? e.g. for (int i = 0; i < 10; i++) {} for (int i = 0; i < 10; ++i) {}
user559142
  • 12,279
  • 49
  • 116
  • 179
83
votes
2 answers

increment value of int being pointed to by pointer

I have an int pointer (i.e., int *count) that I want to increment the integer being pointed at by using the ++ operator. I thought I would call: *count++; However, I am getting a build warning "expression result unused". I can: call *count +=…
joels
  • 7,249
  • 11
  • 53
  • 94
78
votes
14 answers

Post-increment and Pre-increment concept?

I don't understand the concept of postfix and prefix increment or decrement. Can anyone give a better explanation?
Saad Masood
  • 11,036
  • 9
  • 32
  • 40
74
votes
6 answers

Pre- & Post Increment in C#

I am a little confused about how the C# compiler handles pre- and post increments and decrements. When I code the following: int x = 4; x = x++ + ++x; x will have the value 10 afterwards. I think this is because the pre-increment sets x to 5, which…
Schweder
  • 1,464
  • 2
  • 13
  • 19
74
votes
5 answers

Why is "while (i++ < n) {}" significantly slower than "while (++i < n) {}"

Apparently on my Windows 8 laptop with HotSpot JDK 1.7.0_45 (with all compiler/VM options set to default), the below loop final int n = Integer.MAX_VALUE; int i = 0; while (++i < n) { } is at least 2 orders of magnitude faster (~10 ms vs. ~5000 ms)…
63
votes
9 answers

++i or i++ in for loops ??

Possible Duplicate: Is there a performance difference between i++ and ++i in C++? Is there a reason some programmers write ++i in a normal for loop instead of writing i++?
Ismail Marmoush
  • 13,140
  • 25
  • 80
  • 114
62
votes
11 answers

Why doesn't the post increment operator work on a method that returns an int?

public void increment(){ int zero = 0; int oneA = zero++; // Compiles int oneB = 0++; // Doesn't compile int oneC = getInt()++; // Doesn't compile } private int getInt(){ return 0; } They are all int's, why won't B & C…
Blundell
  • 75,855
  • 30
  • 208
  • 233
1
2 3
37 38