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
49
votes
27 answers

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

Why does this int x = 2; for (int y =2; y>0;y--){ System.out.println(x + " "+ y + " "); x++; } prints the same as this? int x = 2; for (int y =2; y>0;--y){ System.out.println(x + " "+ y + " "); …
andandandand
  • 21,946
  • 60
  • 170
  • 271
45
votes
15 answers

a = (a++) * (a++) gives strange results in Java

I'm studying for the OCPJP exam, and so I have to understand every little strange detail of Java. This includes the order in which the pre- and post-increment operators apply to variables. The following code is giving me strange results: int a =…
Marius
  • 57,995
  • 32
  • 132
  • 151
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
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
38
votes
3 answers

String concatenation while incrementing

This is my code: $a = 5; $b = &$a; echo ++$a.$b++; Shouldn't it print 66? Why does it print 76?
user2500553
  • 353
  • 3
  • 6
33
votes
3 answers

Pre increment vs Post increment in array

I am learning programming and I have started from C language. I was reading Let us C book. And I was going through this program in that book. main( ) { int a[5] = { 5, 1, 15, 20, 25 } ; int i, j, k = 1, m ; i = ++a[1] ; j = a[1]++ ; m =…
user2442489
30
votes
7 answers

Is the behavior of return x++; defined?

If I have for example a class with instance method and variables class Foo { ... int x; int bar() { return x++; } }; Is the behavior of returning a post-incremented variable defined?
patros
  • 7,719
  • 3
  • 28
  • 37
29
votes
8 answers

Why can't I do ++i++ in C-like languages?

Half jokingly half serious : Why can't I do ++i++ in C-like languages, specifically in C#? I'd expect it to increment the value, use that in my expression, then increment again.
Andriy Volkov
  • 18,653
  • 9
  • 68
  • 83
24
votes
6 answers

Strange Increment Behaviour in C#

Note: Please note that the code below is essentially non-sense, and just for illustration purposes. Based on the fact that the right-hand side of an assignment must always be evaluated before it's value is assigned to the left-hand side variable,…
User
  • 3,244
  • 8
  • 27
  • 48
21
votes
5 answers

Post increment operator not incrementing in for loop

I'm doing some research about Java and find this very confusing: for (int i = 0; i < 10; i = i++) { System.err.print("hoo... "); } This is never ending loop! Anybody has good explanation why such thing happens?
Michał Kupisiński
  • 3,765
  • 1
  • 19
  • 23
20
votes
7 answers

Incrementor logic

I'm trying to get deeper with post and pre incrementors but am a bit stuck with the following expression : public static void main(String[] args) { int i = 0; i = i+=(++i + (i+=2 + --i) - ++i); // i = 0 + (++i + (i+=2 + --i) - ++i); …
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
20
votes
8 answers

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…
user1886376
19
votes
6 answers

Java increment and assignment operator

I am confused about the post ++ and pre ++ operator , for example in the following code int x = 10; x = x++; sysout(x); will print 10 ? It prints 10,but I expected it should print 11 but when I do x = ++x; instead of x = x++; it will print…
user3803547
  • 205
  • 1
  • 2
  • 5
17
votes
5 answers

Post Increment in while loop in C

Here is a very simple C program: int main() { int i = 0; while(i++ < 10) printf("%d\n", i); return 0; } The result is: 1 2 3 4 5 6 7 8 9 10 Why 0 is not the first number to print? And if I replace the i++ with ++i I'll get…
algo
  • 197
  • 1
  • 1
  • 5
1
2
3
37 38