Questions tagged [pre-increment]

For issues relating to defining or performing pre increment operations.

Pre-increment operators increase the value of their operand by 1 (or another specified amount), and the value of the operand in the expression is the operand's value after the increment operation.

342 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
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
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
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
45
votes
5 answers

Preincrement faster than postincrement in C++ - true? If yes, why is it?

I heard about that preincrements (++i) are a bit faster than postincrements (i++) in C++. Is that true? And what is the reason for this?
user163408
45
votes
5 answers

Expressions "j = ++(i | i); and j = ++(i & i); should be a lvalue error?

I was expecting that in my following code: #include int main(){ int i = 10; int j = 10; j = ++(i | i); printf("%d %d\n", j, i); j = ++(i & i); printf("%d %d\n", j, i); return 1; } expressions j = ++(i |…
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
42
votes
6 answers

Post-increment within a self-assignment

I understand the differences between i++ and ++i, but I'm not quite sure why I'm getting the results below: static void Main(string[] args) { int c = 42; c = c++; Console.WriteLine(c); //Output: 42 } In the above code, as this is…
Siyual
  • 16,415
  • 8
  • 44
  • 58
38
votes
11 answers

Java: Prefix/postfix of increment/decrement operators

From the program below or here, why does the last call to System.out.println(i) print the value 7? class PrePostDemo { public static void main(String[] args) { int i = 3; i++; System.out.println(i); // "4" …
O_O
  • 4,397
  • 18
  • 54
  • 69
1
2 3
22 23