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

Is the pre-increment operator thread-safe?

I'm making a program in java that races a few cars against each other. Each car is a separate thread. When cars complete the race, each one all calls this method. I've tested the method at varying timer speeds, and it seems to work fine. But I do…
TPXL
  • 327
  • 1
  • 3
  • 12
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
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
17
votes
5 answers

Java: pre-,postfix operator precedences

I have two similar questions about operator precedences in Java. First one: int X = 10; System.out.println(X++ * ++X * X++); //it prints 1440 According to Oracle tutorial: postfix (expr++, expr--) operators have higher precedence than prefix…
D.R.
  • 175
  • 2
  • 9
17
votes
3 answers

Is ++x more efficient than x++ in Java?

During a programming class, the professor was teaching us about x++ and ++x, with x being an integer. He said that in the scenario we are able to just put either x++ or ++x, ++x is more efficient (by little, but still, in theory, more efficient…
Saturn
  • 17,888
  • 49
  • 145
  • 271
15
votes
20 answers

What is more efficient, i++ or ++i?

Exact Duplicate: Is there a performance difference between i++ and ++i in C++? Exact Duplicate: Difference between i++ and ++i in a loop? What is more efficient, i++ or ++i? I have only used this in Java and C/C++, but I am really asking for all…
Berek Bryan
  • 13,755
  • 10
  • 32
  • 43
15
votes
2 answers

Multiple preincrement operations on a variable in C++(C ?)

Why does the following compile in C++? int phew = 53; ++++++++++phew ; The same code fails in C, why?
Jatin
  • 161
  • 1
  • 4
14
votes
3 answers

Why is "++i++" invalid while (++i)++ is valid?

Let's consider the following code: int main() { int i = 2; int b = ++i++; return 3; } It compiles with the following with an error: : In function 'int main()': :3:16: error: lvalue required as increment operand 3 |…
Bktero
  • 722
  • 5
  • 15
14
votes
6 answers

Increment operator inside array

I have a C program which does queue operations using an array. In that program, they increment a variable inside array. I can't understand how that works. So, please explain these operations: array[++i]; array[i++];
Loki San Hitleson
  • 177
  • 1
  • 2
  • 7
14
votes
2 answers

incrementing struct members

Say I have a struct defined as follows struct my_struct { int num; }; .... Here I have a pointer to my_struct and I want to do an increment on num void foo(struct my_struct* my_ptr) { // increment num // method #1 …
user1508893
  • 9,223
  • 14
  • 45
  • 57
13
votes
2 answers

Is there a performance difference between i++ and ++i in JavaScript?

I read Is there a performance difference between i++ and ++i in C?: Is there a performance difference between i++ and ++i if the resulting value is not used? What's the answer for JavaScript? For example, which of the following is better? for…
Oriol
  • 274,082
  • 63
  • 437
  • 513
1
2
3
22 23