difference between post and pre increment op

what is diffrence amoung
x=25;
x=x++ + ++x;
and
cout< and
y=x++ + ++x;
cout<
plz explain me the concept

Replies

  • chotaprogramer
    chotaprogramer
    i have same problem so is their any one who can solve our problem?
  • chotaprogramer
    chotaprogramer
    its is fine but tell me if i do
    this
    y=x++ + ++x;
    and
    x=x++ + ++x;
    ten it gives me different outputs so tell me what is that concept behind it
  • shalini_goel14
    shalini_goel14
    chotaprogramer
    its is fine but tell me if i do
    this
    y=x++ + ++x;
    and
    x=x++ + ++x;
    ten it gives me different outputs so tell me what is that concept behind it
    Hi
    An attempt to explain the concept for following scenario.

    x=25
    x= x++ + x++

    It will be taken as
    x =(x++) + (++x)
    LHS= x
    RHS= (x++) + (++x)

    Taking RHS
    (x++) will take first operand as 25 but increments it to 26 later because post increment operator. so first operand will be 25 but x will be 26

    (++x) will take x as 26 as calculated by first operand and before assigning this value to second operand, it will increment by 27 because it is pre increment operator so ultimately second operand will be 27

    SO now your RHS= First operand + second operand
    RHS= 25 +27 =52
    LHS=RHS (assignment operator)
    x=52
    so final value of x=52.

    Hope this may have cleared your doubt.Now you can apply same concept for any kind of scenario.

    PS: Anyone Please correct me if anything wrong.๐Ÿ˜€

    Thanks
  • pradeep_agrawal
    pradeep_agrawal
    shalini_goel14
    x=25
    x= x++ + x++

    It will be taken as
    x =(x++) + (++x)
    LHS= x
    RHS= (x++) + (++x)

    ...

    so final value of x=52.

    When i compiled and executed the above code using gcc compiler the final result was x = 53. So i feel the logic described above is not correct.

    Also, why should the statement
    x = x++ + ++x;
    should evaluate to
    x = (x++) + (++x);
    and not to
    x = (x++++) + x;
    or
    x = x + (++++x);

    The reason it does not evaluate to
    x = (x++++) + x;
    or
    x = x + (++++x);
    is that in both the case the compiler will throw a error that lvalue is required because it does not have any lvalue on which it can store the result of second '++' operator (the '++' away from x in x++++ or ++++x).


    Now why it results in 53 and not 52? I feel below details should answer this.


    Before trying to understand the behavior we should have below understanding:
    - In case of ++ (pre-increment operator, ++x) in any statement, it always first increment the value and then used the new value in the statement.
    - In case of ++ (post-increment operator, x++) the value is first used in the statement and after completion of statement the value is incremented.


    So the above statements are evaluated as:

    Statement: y = x++ + ++x;

    1. x = 25
    2. Evaluating pre-increment (++x)
    Statement: y = x++ + 26
    and x = 26
    3. Evaluating post-increment (x++)
    Statement: y = 26 + 26
    and x = 26 (with x need to be incremented after execution of whole statement)
    4. Evaluating addition (+)
    Statement: y = 52
    and x = 26 (with x need to be incremented after execution of whole statement)
    5. Evaluating assignment
    y = 52
    x = 26 (with x need to be incremented after execution of whole statement)
    6. Final values
    y = 52
    x = 27


    Statement: x = x++ + ++x;

    1. x = 25
    2. Evaluating pre-increment (++x)
    Statement: x = x++ + 26
    and x = 26
    3. Evaluating post-increment (x++)
    Statement: x = 26 + 26
    and x = 26 (with x need to be incremented after execution of whole statement)
    4. Evaluating addition (+)
    Statement: x = 52
    and x = 26 (with x need to be incremented after execution of whole statement)
    5. Evaluating assignment
    x = 52 (with x need to be incremented after execution of whole statement)
    6. Final values
    x = 53


    Here we evaluated ++x first and x++ later (i.e., right portion of '+' first and left portion later). This behavior is compiler dependent. While evaluating any binary operator some compilers generate code to evaluate right portion first and some to evaluate left portion first (gcc do to evaluate right portion first). Other compilers may give different result. That's why it is recommended to avoid coding that's compiler dependent. This makes code easily portable.


    Let me know if any item need more clarification.

    -Pradeep
  • Prasad Ajinkya
    Prasad Ajinkya
    Very nicely done Pradeep ๐Ÿ˜€
  • shalini_goel14
    shalini_goel14
    Hey Mr Pradeep

    Any ideas why the output is 52 in java and not 53? ๐Ÿ˜•
  • pradeep_agrawal
    pradeep_agrawal
    shalini_goel14
    Any ideas why the output is 52 in java and not 53?
    I feel in Java the left portion of binary operator is evaluated first and then right portion. So x++ will be evaluated first and the ++x. So the computation will be as:

    Statement: x = x++ + ++x;

    1. x = 25
    2. Evaluating post-increment (x++)
    Statement: x = 25 + ++x
    and x = 25 (with x need to be incremented after execution of whole statement)
    3. Evaluating pre-increment (++x)
    Statement: x = 25 + 26
    and x = 26 (with x need to be incremented after execution of whole statement)
    4. Evaluating addition (+)
    Statement: x = 51
    and x = 26 (with x need to be incremented after execution of whole statement)
    5. Evaluating assignment
    x = 51 (with x need to be incremented after execution of whole statement)
    6. Final values
    x = 52

    This will result answer as 52.

    -Pradeep
  • shalini_goel14
    shalini_goel14
    Thanks for clearing but are you sure these explanations are correct? ๐Ÿ˜€ I mean for the first time I saw such explanation of increment operators.

    Please confirm it once so that we poor people are not misguided.๐Ÿ˜”

    Thanks
  • prettyprasu
    prettyprasu
    Thank you for the explanation.. it was so useful to me.
  • Richard Hanson
    Richard Hanson
    compiling with Dev-Cpp\MinGW64\bin\g++.exe

    int x {5} ;
    x = ++x ; // evaluates to 6
    a: ++x : increment x so x = 6
    b: assignment x = x ; so x = 6

    int x {5} ;
    x = x++ ; // evaluates to 5
    a: x++ : make a copy of x ( tmpx = x = 5 ) and then increment x so x = 6
    b: assignment x = tmpx ; so x reverts to 5, x = 5

    int x {5} ;
    x = ++x + ++x ; // evaluates to 14
    a: right ++x : increment x so x = 6
    b: left ++x : increment x so x = 7
    d: addition and assignment x = x + x that is x = 7 + 7 = 14

    int x {5} ;
    x = ++x + x++ ; // evaluates to 13
    a: ++x : increment x so x = 6
    b: x++ : make a copy of x ( tmpx = x = 6 ) and increment x so x = 7
    c: addition and assignment x = x + tmpx that is x = 7 + 6 = 13

    int x {5} ;
    x = x++ + ++x ; // evaluates to 12
    a: x++ : make a copy x (tmpx = x = 5 ) and increment x so x = 6;
    b: ++x : increment x so x = 7
    c: addition and assignment x = tmpx + x = 5 + 7 = 12;

    int x {5} ;
    x = x++ + x++ ; // evaluates to 11;
    a: right x++ : make a copy of x ( tempxr = x = 5 ) and increment x so x = 6
    b: left x++ : make a copy of x ( tempxl = x = 6 ) and increment x so x = 7
    c: addition and assignment x = tempxr + tempxl = 5 + 6 = 11

    int x {5} ;
    x = ++++x ; // evaluates to 7
    grouping right to left of ++++ so x = ++(++x) ;
    a: ++x : increment x so x = 6 and x is a left hand value a lvalue
    b: ++x : increment x so x = 7 and x is still a lvalue

    int x {5} ;
    x = x++++ ; // [Error] lvalue required as increment operand
    grouping left to right of ++++ so x = (x++)++ ;
    a: x++ : make a copy of x ( tmpx = x = 5 ) and increment x so x = 6
    we now have x = tmpx++ ; but tmpx is a temporary value and not a lvalue

    int x {5} ;
    x = ++x++ ; // [Error] lvalue required as increment operand
    postfix has higher precedence that prefix x = ++(x++) ;
    a: x++ : make a copy of x ( tmpx = x = 5 ) and increment x so x = 6
    we now have x = ++tmpx ; but tmpx is a temporary value and not a lvalue.

    int x {5} ;
    x = (++x)++ ; // evaluates to 6;
    a: ++x : increment x so x = 6
    b: x++ : save a copy of x ( tmpx = x = 6 ) and increment x so x = 7
    c: assignment x = tmpx = 6

You are reading an archived discussion.

Related Posts

Hello, My company is currently looking for an experienced electronics engineer to continue a project for a battery management system (preferably in the general New York City area). If you...
could someone help me about this folder option because I accidentally hid my folder in my drive d.. and I can't unhide it because I can't see it at the...
Hello!I'm new here. I've been browsing tons of forums for an idea of what to do. I was laid off from my job about 3 month ago. I've been looking...
Hello all, I am Vivek, I have a paper presentation competition coming up in my college. Let me know some good topics for Presentation ppt in Computer Science and Engineering....
Hey,, am thinking to do Final year Project on some Simulator .. on some Error correction or some other techniques.. please... Suggest me some topics relating to Simulators.. orite.. my...