-3

We can define a++ as

a = a + 1;

Which is self-explaining that it makes a copy of a then adds 1 to it and puts the result in a.

But can we define ++a the same way? Maybe the answer is very simple but I'm unaware of it.

5gon12eder
  • 24,280
  • 5
  • 45
  • 92
SSH
  • 1,609
  • 2
  • 22
  • 42
  • 2
    If you only care about the value of `a` after the statement has executed, then `a++` and `++a` are the same. It's the result of the expression that is different but your “definition” doesn't care about that. I also wouldn't call it “mathematical” because the mathematical formula *a* = *a* + 1 doesn't make any sense. – 5gon12eder Sep 18 '15 at 02:36
  • 1
    Apparently you've switched `a++` and `++a`. It's `++a`, the prefix operator, that can be replaced with `(a = a+1)`. For `a++`, the postfix operator, the expression result is the original value of `a`, and you can't define that as an expression without using another postfix operator. – Cheers and hth. - Alf Sep 18 '15 at 02:41
  • 1
    For the mathematical view of things you *can* consider `a` as an infinite sequence of values, namely the values at each point in time. But that's not a practical viewpoint for C++ programming. – Cheers and hth. - Alf Sep 18 '15 at 02:42

2 Answers2

0

a++ and ++a have differences in precedence. - a++: you evaluate a before a is incremented; - ++a: you increment a before it is evaluated or used

Ricky C.
  • 21
  • 2
0

The difference is:

int a = 1; 
int b = ++a; 
// Now a == 2 and b == 2 

int c = 1; 
int d = c++;
// Now c == 2 and d == 1

The difference is in the returned value of the operation.

Rodolfo
  • 1,091
  • 3
  • 13
  • 35
  • Thnks fr the reply ,i knw the difrnce,m nt asking for it,wat m asking is can we show the functionality of ++a with a simple mathematical equation wch doesnt need any wordly xplanation – SSH Sep 18 '15 at 02:46