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

Pre increment in Javascript

I've just encountered a 'feature' in Javascript regarding pre-increments. In all other languages I've used, it goes like I thought it would. E.g. in C++: #include int main() { int i = 0; i += ++i; std::cout << i <<…
nhaa123
  • 9,570
  • 11
  • 42
  • 63
10
votes
3 answers

Why post-increment needs to make a copy while pre-increment does not

I know this issue has been discussed several times , but I could not find a post which explains why a copy needs to be made in case of a post-increment operation. Quoting from a stackoverflow reply: int j = i++; // j will contain i, i will be…
ralzaul
  • 4,280
  • 6
  • 32
  • 51
10
votes
6 answers

What are the historical reasons C languages have pre-increments and post-increments?

(Note: I am not asking about the definitions of pre-increment vs. post-increment, or how they are used in C/C++. Therefore, I do not think this is a duplicate question.) Developers of C (Dennis Ritchie et al) created increment and decrement…
ShanZhengYang
  • 16,511
  • 49
  • 132
  • 234
10
votes
4 answers

i++ vs. ++i in a JavaScript for loop

Because of JSLint, I almost always use i += 1 to increment a JavaScript for loop, but for quick and dirty scripts, I use i++ instead. However, I see a lot of for loops in other people's code in which they increment i by doing ++i instead. As far as…
HartleySan
  • 7,404
  • 14
  • 66
  • 119
9
votes
4 answers

Order of operations for pre-increment and post-increment in a function argument?

I have some C code: main() { int a=1; void xyz(int,int); xyz(++a,a++); //which Unary Operator is executed first, ++a or a++? printf("%d",a); } void xyz(int x,int y) { printf("\n%d %d",x,y); } The function xyz has two…
thulasi
  • 109
  • 1
  • 1
  • 5
9
votes
3 answers

What should be the output of echo ++$a + $a++

In the PHP manual, operator precedence section, there is this example: // mixing ++ and + produces undefined behavior $a = 1; echo ++$a + $a++; // may print 4 or 5 I understand the behavior is undefined because of the following reason: Since x + y…
Salman A
  • 262,204
  • 82
  • 430
  • 521
9
votes
3 answers

How does expression evaluation order differ between C++ and Java?

I've had my brain wrinkled from trying to understand the examples on this page: http://answers.yahoo.com/question/index?qid=20091103170907AAxXYG9 More specifically this code: int j = 4; cout << j++ << j << ++j << endl; gives an output: 566 Now this…
Chironex
  • 809
  • 3
  • 10
  • 28
8
votes
6 answers

Difference between i = ++i and ++i

Possible Duplicate: Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…) What is the difference between i = ++i; and ++i; where i is an integer with value 10? According to me both do the same job of incrementing i i.e…
8
votes
4 answers

Java post-increment (++) not behaving as expected when passed as a parameter

I came across the following issue: private void doStuff(int i) { if(i>10) { return; } doStuff(i++); } public void publicMethod() { doStuff(i); } I would expect this to run doStuff 10 times and then return. However i++ does not…
dngfng
  • 1,923
  • 17
  • 34
8
votes
4 answers

The assignment to variable has no effect?

When I do this: count = ++count; Why do i get the warning - The assignment to variable count has no effect ? This means that count is incremented and then assigned to itself or something else ? Is it the same as just ++count ? What happens in…
sweet dreams
  • 2,094
  • 13
  • 32
  • 46
7
votes
4 answers

Overloaded 'operator++' must be a unary or binary operator (has 3 parameters)

I have a header file and a .cpp file. I am trying to implement a prefix and postfix operator overload but I keep getting this error when setting up the overload. fraction.h #ifndef FRACTION_H #define FRACTION_H #include using…
6
votes
1 answer

Difference between &++x and &x++

Although this question might be answered somewhere and I could not find it. Below written first statement work whereas second does not? WHY? int main() { int x = 1, y = 2; int *p = &++x; // First std::cout << "*p = " << *p << std::endl; //…
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
1 2
3
22 23