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
6
votes
2 answers

How to implement pre and post increment in Python lists?

In Python how can we increment or decrement an index within the square braces of a list? For instance, in Java the following code array[i] = value i-- can be written as array[i--] In Python, how can we implement it? list[i--] is not working I…
Sriram
  • 999
  • 2
  • 12
  • 29
6
votes
2 answers

When to use post increment and pre increment in Java

I understand that there are a number of questions on this topic on StackOverflow. But I am still slightly confused and unsure of when to use the operations. I am going through old tests in studying for my exam. One of the methods returns the number…
user1454994
  • 303
  • 3
  • 5
  • 12
6
votes
3 answers

In a for loop, is there a difference between pre/post-incrementing a loop control variable in terms of the total quantity of iterations?

When I compile and run the code below with either counter++ or ++counter substituted for x, the output is identical; in both cases, numbers 1 - 10: for (int counter = 1; counter < 11; x) { std::cout << counter << endl; } Originally I thought…
izaak_pyzaak
  • 930
  • 8
  • 23
6
votes
3 answers

Why the expression a==--a true in if statement?

#include int main() { int a = 10; if (a == a--) printf("TRUE 1\t"); a = 10; if (a == --a) printf("TRUE 2\t"); } Why is the second if statement true? Output is: …
Pankaj Mahato
  • 1,051
  • 5
  • 14
  • 26
6
votes
2 answers

Behaviour of PreIncrement and PostIncrement operator in C and Java

I'm running the following programs in Visual C++ and Java: Visual C++ void main() { int i = 1, j; j = i++ + i++ + ++i; printf("%d\n",j); } Output: 6 Java: public class Increment { public static void main(String[] args) { …
Jainendra
  • 24,713
  • 30
  • 122
  • 169
6
votes
1 answer

Dereference-assignment to a doubly incremented OutputIterator

Per the (excellent) question C++ OutputIterator post-increment requirements, we observe that for a dereferenceable and incrementable value r of OutputIterator type X, and value o of appropriate type, the expression *r++ = o; is valid and has…
ecatmur
  • 152,476
  • 27
  • 293
  • 366
5
votes
5 answers

Increment operator returns NaN

I am trying to increment a variable using the ++ operator but I keep getting NaN as a result and I'm not sure why. Here is my code: var wordCounts = { }; var x = 0 var compare = "groove is in the heart"; var words = compare.split(/\b/); …
mcgrailm
  • 17,469
  • 22
  • 83
  • 129
5
votes
3 answers

Does i = x[i]++; lead to undefined behavior?

Can someone please explain whether i = x[i]++; lead to undefined behavior? Note: x[i] and i are not both volatile and x[i] does not overlap i. There is C11, 6.5 Expressions, 2 (emphasis added): If a side effect on a scalar object is unsequenced…
pmor
  • 5,392
  • 4
  • 17
  • 36
5
votes
3 answers

C Incrementing pointer address passed to a function ++ operator

I have a question that is raised from this discussion: C - modify the address of a pointer passed to a function Let's say I have the following code: #include foo(char **ptr){ *ptr++; } int main() { char *ptr = malloc(64); …
asevindik
  • 131
  • 9
5
votes
1 answer

Why can't a++ (post-increment operator) be an Lvalue?

Code #include int main() { int a=3; a++=5; std::cout<
Abhishek Mane
  • 619
  • 7
  • 20
5
votes
1 answer

unexpected output of C++

I found unexpected output of following program. Here pointer ptr point to address of variable i and i hold the value 10. It means the value of ptr also 10. Next ptr increment once. It means now it hold value 11. But in the following program ptr…
msc
  • 33,420
  • 29
  • 119
  • 214
5
votes
3 answers

Closure Compiler - can a++ >= 3 become ++a > 3?

I admit that I asked a question about why Closure Compiler does not shorten certain code which looks shortenable at first sight a few days ago already, but that reason is not applicable in this case and I'm not really sure why it isn't shortened…
pimvdb
  • 151,816
  • 78
  • 307
  • 352
5
votes
4 answers

Operator associativity with 'postfix decrement' and 'logical AND' operators in c

Disclaimer: I don't code like this, I'm just trying to understand how the c language works!!!! The output is 12. This expression (a-- == 10 && a-- == 9) evaluates left-to-right, and a is still 10 at a-- == 10 but a is 9 for a-- == 9. 1) Is there a…
5
votes
2 answers

Post increment behavior

Recently, I upgraded my project from gcc 4.3 to gcc 5.5. After that, I see a change of behaviour in the post-increment operator which is causing issues in my project. I am using a global variable as a control variable. For example, consider this…
5
votes
2 answers

What does this arithmetic expression mean: A += B++ == 0 in C++;

I came accross this expression, and can't understand the meaning of line 3 in the following snippet: int A=0, B=0; std::cout << A << B << "\n"; // Prints 0, 0 A += B++ == 0; // how does this exp work exactly? std::cout << A << B << "\n"; // Prints…
Batwoman05
  • 215
  • 1
  • 2
  • 9