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
5
votes
3 answers

Why have for-loops with pre-increment the same behaviour as those with post-increment?

It's probably fair to say everyone learns writing for-loops using post-increment: for(var i = 0; i < 10; i++) { console.log(i); // 0..9 } When I swap the post-increment out for a pre-increment, I'd expect the following: for(var i = 0; i < 10;…
Christian
  • 6,070
  • 11
  • 53
  • 103
5
votes
1 answer

pre-increment vs post-increment - for-loop speed

I never saw a tutorial or some lecture, which showed a classic for-loop witout the post-increment-order. for (int i=0; i
codepleb
  • 10,086
  • 14
  • 69
  • 111
5
votes
4 answers

Why does this cause an infinite loop

Consider this simple code: // E1 public void doTest(String pattern) { int counter = 0; while (counter < 3) { counter = counter++; } System.out.println("Done"); } This causes an infinite loop. However if the statement…
Elliott
  • 5,523
  • 10
  • 48
  • 87
5
votes
7 answers

Why is this Java operator precedence being ignored here?

The following code prints out "3", not "4" as you might expect. public class Foo2 { public static void main(String[] args) { int a=1, b=2; a = b + a++; System.out.println(a); } } I understand how. The…
Aaron Fi
  • 10,116
  • 13
  • 66
  • 91
5
votes
6 answers

How many primitive steps in y=x++?

y=x++; In how many steps does it break at compiler level without optimization not at CPU level or instructions ? Does any temporary is created to assign x to y or, it happens directly ?
Omkant
  • 9,018
  • 8
  • 39
  • 59
5
votes
7 answers

Post-increment and pre-increment operator in C

Why is k not getting incremented whereas,i and j are getting incremented in the same expression.And i also want to know what is the output of the program.I am getting the output as -2 3 1 0 #include void main() { int i=-3, j=2, m, k=0; …
Rishab777
  • 565
  • 6
  • 14
5
votes
5 answers

Why is i=i+1 faster than i++?

Test this code in Flash: var i:int = 0; for (var j:int = 0; j < 5000000; j++) { i=i+1; }// use about 300ms. i = 0; for (var j:int = 0; j < 5000000; j++) { i++; }// use about 400ms i = 0; for (var j:int = 0; j < 5000000; j++) { ++i; }//…
5
votes
3 answers

Post increment and Pre increment in C

I have a question about these two C statements: x = y++; t = *ptr++; With statement 1, the initial value of y is copied into x then y is incremented. With statement 2, We look into the value pointed at by *ptr, putting that into variable t, then…
Pkp
  • 929
  • 6
  • 19
  • 31
4
votes
2 answers

Java pre and post incrementing

I am having trouble understanding the following code block. int count = 0; for (int i = 0; i < 3; i++){ count += (count++); System.out.println("count = " + count); System.out.println("i = " + i); } My understanding is…
4
votes
3 answers

Does a variable holding result of signed integer overflow (side effect of post incr.) and, after that, never used it in any expression, result in UB?

Consider this program #include int main (void) { int i = 0; // Assume, user is a fair person, following the instruction strictly.. printf ("Enter a number in the range [0 - INT_MAX] : \n"); scanf ("%d", &i); while…
H.S.
  • 11,654
  • 2
  • 15
  • 32
4
votes
0 answers

PHP incrementing operator

Might be a duplicate, but I never found an answer. I ran some basic tests with incrementing operator: $i = 42; $i > ++$i; # false $i > $i++; # true If the first comparison makes sense to me returning false, I can not figure out why the…
4
votes
3 answers

Post-increment operator behaviour w.r.t comma operator?

In the following code: int main() { int i, j; j = 10; i = (j++, j+100, 999+j); cout << i; return 0; } The output is 1010. However shouldn't it be 1009, as ++ should be done after the whole expression is used?
Anon
  • 381
  • 1
  • 2
  • 13
4
votes
2 answers

When are the ++c and c++ increments applied exactly here?

Just to see how much I understood how the ++c/c++ operator works, I tried to run these C programs: int c = 5; c = c - c++; printf("%d\n", c); prints 1, I guess the logic is that the ++ is applied after the line of code where it's used, so c becomes…
memememe
  • 663
  • 6
  • 21
4
votes
4 answers

Java is said to ignore extra whitespace. Why does c=a++ + ++b not compile without the spaces?

In all books on Java, I've read that the compiler treats all whitespace in the same way and simply ignores extra whitespace, so it's best practice to use them liberally to improve code readability. I've found proof to that in every expression that…
4
votes
1 answer

PHP Post and preincrement

I have found in PHP some strange calculation, for example this: $c=5; $r = $c + ($c++ + ++$c); echo $r; Why result is 19 and not 17? Thanks
NeroBuggy
  • 67
  • 2