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

Difference between b=b++ and b++

I was asked in an interview the below question. int b = 0; b = b++; b = b++; b = b++; b = b++; what will be the value of b after every line execution ? The output is coming as 0 for every line. Why is the output not coming as 0,1,2,3 ?
robin
  • 1,893
  • 1
  • 18
  • 38
3
votes
3 answers

whether a language needs preIncrement (++x) and postIncrement (x++)

I have never seen the usecase for pre-increment and post-increment in actual code. The only place i see them most often are puzzles. My opinion is, it introduces more confusion rather than being useful. is there any real use case scenario for…
3
votes
0 answers

Java increment operator

I'm reading Sams Teach Yourself Java and on the page about incrementing and decrementing the author says int x = 7; x = x++; "In this example, the statement x = x++ increments the x variable from 7 to 8." However the output is not 8, but 7…
some random dude
  • 457
  • 1
  • 4
  • 11
3
votes
2 answers

Follow-up. Is return reference to x++ defined?

I recently asked the question Is the behavior of return x++; defined? The result was about what I expected, but got me thinking about a similar situation. If I were to write class Foo { ... int x; int& bar() { return x++; } }; Where…
patros
  • 7,719
  • 3
  • 28
  • 37
3
votes
4 answers

Confusing output after use of increment operator

#include main() { int a[5] = {5,1,15,20,25}; int i,j,m; i = ++a[1]; j = a[1]++; m = a[i++]; printf("%d %d %d\n",i,j,m); } Okay now the program compiles and runs fine. But the output which i get is 3 2 15 i.e i…
3
votes
1 answer

Is there a difference between ++i and i++ in this loop?

The array.prototype.reduce function at: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce It has the following loop: for (index = 0; length > index; ++index) { if (this.hasOwnProperty(index)) { …
ciso
  • 2,887
  • 6
  • 33
  • 58
3
votes
6 answers

How does the increment operator work in an if statement?

#include int main() { int x = 0; if (x++) printf("true\n"); else if (x == 1) printf("false\n"); return 0; } Output: false Why is the output false? x++ is post…
Pankaj Mahato
  • 1,051
  • 5
  • 14
  • 26
3
votes
2 answers

Why incrementing int primitive in while loop does not loop forever

I have a following code sample int i = 1; while(i != 0) { i++; } I was expecting this to run in an infinite loop but it didn't. Then when I printed the value after while loop I got: i value is 0. Can any one let me know what…
Srikanth Ganji
  • 1,127
  • 1
  • 13
  • 29
3
votes
9 answers

lvalue and rvalue assignment error

int x = 3; int y = 5; x++ = y; cout << x << endl; Why is the above code giving me an error, the specific error I get is lvalue required as left operand of assignment. I am trying to review up on my C and C++. From my understanding the above code…
AyBayBay
  • 1,726
  • 4
  • 18
  • 37
3
votes
3 answers

Reference-type return functions and postfix increment

I am reading the Wikipedia page about references. It contains the following code: int& preinc(int& x) { return ++x; // "return x++;" would have been wrong } preinc(y) = 5; // same as ++y, y = 5 I did try to compile using return x++; instead…
usual me
  • 8,338
  • 10
  • 52
  • 95
3
votes
3 answers

why same code in two technology behaving different

Below is my code snippet in C. void main(){ int x = 7; x = x++; printf("%d",x); } output : 8 public static void main(String[] args){ int x = 7; x = x++; System.out.println(x); } output : 7 i am not getting why…
ved
  • 909
  • 2
  • 17
  • 43
3
votes
3 answers

K&R seems to prefer pre-increment

I'm working through K&R and am presently on Exercise 1-16. It occurs to me that thus far only pre-increment has been used in the book. Most other tutorial books and indeed source code I've seen tend to favour post-increment, except where there is an…
retrodev
  • 2,323
  • 6
  • 24
  • 48
3
votes
7 answers

Semantics of pre- and postfix "++" operator in Java

I wondering to know why this snippet of code give output 112 How this last digit 2 was creating? public static void main(String[] args) { int i = 0; System.out.print(++i); System.out.print(i++); System.out.print(i); Why does this…
user2374612
  • 115
  • 1
  • 4
3
votes
4 answers

What happens to the increment of "b" in "return b++" ,if used in a function?

Since b++ is post-increment,what happens to the increment of b if used as return b++ as in the following program? #include int foo(int); int main() { int a=8; printf("%d",foo(a)); } int foo(int a) { static int b=a*a; return…
Thokchom
  • 1,602
  • 3
  • 17
  • 32
3
votes
2 answers

Post-Incrementing/decrementing in recursive method calls (Java)

Say you have a recursive method, and you post-increment/decrement a value in the recursive call. Why will this result in a stack overflow exception when a pre-increment/decrement will not? Ex. numberCount(currentNumber++); //Stack overflow…
Derrek Whistle
  • 701
  • 2
  • 7
  • 16