0

I know If the operator is placed before the operand, referred to as the pre-increment operator and the pre-decrement operator,then the operator is applied first and the value return is the new value of the expression.

Alternatively,if the operator is placed after the operand, referred to as the post-increment operator and the post-decrement operator,then the original value of the expression is returned,with operator applied after the value is returned.

But i dont understand below codes output.

//CODE FRAGMENT A

int value1 = 3;
int value2 = ++value1 * 5 / value1-- + --value1;
//  value2 =  4 * 5 / value1-- + --value1;
//  value2 =  4 * 5 / 4 + --value1;
//  value2 =  4 * 5 / 4 + 2;
//  value2 =  20 / 4 + 2;
//  value2 =  5 + 2;
//  value2 = 7;

// CODE FRAGMENT B

int var1 = 10;
var1 = var1++ + var1 + var1-- - var1-- + ++var1;
// var1 = 10  + 11   +  11    -  10    +  10;
// var1 = 32

// CODE FRAGMENT C

int var2 = 5;
var2 += var2 + var2 + ++var2; 
// var2 = var2 + var2 + var2 + ++var2;
// var2 =  5   +  5   +  5   +   6;
// var2 = 10 + 5 + 6;
// var2 = 15 + 6;
// var2 = 21;

I know top priority operator post-unary operator and pre-unary operators but i dont understand 'CODE FRAGMENT B & CODE FRAGMENT C' situation.Why this situation is not evaluated as follows ?

// CODE FRAGMENT B
int var1 = 10;
var1 = var1++ + var1 + var1-- - var1-- + ++var1;
// var1 = 10  + var1 +  11    -  10     +  10
// var1 = 10  +  10  +  11    -  10     +  10
// var1 = 20  + 11 - 10 + 10
// var1 = 31 -10 + 10
// var1 = 21 + 10
// var1 = 31

// CODE FRAGMENT C
int var2 = 5;
var2 += var2 + var2 + ++var2; 
// var2 = var2 + var2 + var2 + ++var2;
// var2 = var2 + var2 + var2 +  6;
// var2 =  6   +  6   +  6   +  6;
// var2 = 12 + 6 + 6;
// var2 = 18 + 6;
// var2 = 24;
Surabaya
  • 5
  • 4
  • 6
    What unholy pit of dark magic did you find this line of code in? I think we as a society have a responsibility to bury it and never look at it again. – Silvio Mayolo Mar 25 '21 at 19:54
  • haha I definitely agree with you,I am studying for the certification exam, I did not fully understand this part,I shared it because maybe it will be asked in the exam – Surabaya Mar 25 '21 at 20:27

1 Answers1

1

Here is the logic for code fragment B, showing the updated value of var1 in parentheses.

int var1 = 10;
var1 = var1++ + var1 + var1-- - var1-- + ++var1;
       var1++ = 10  (var1 = 11)
var1 =   10   + var1 + var1-- - var1-- + ++var1;
                var1 = 11  (var1 = 11)
var1 =   10   +  11  + var1-- - var1-- + ++var1;
var1 =       21      + var1-- - var1-- + ++var1;
                       var1-- = 11  (var1 = 10)
var1 =       21      +   11   - var1-- + ++var1;
var1 =             32         - var1-- + ++var1;
                                var1-- = 10  (var1 = 9)
var1 =             32         -   10   + ++var1;
var1 =                 22              + ++var1;
                                         ++var1 = 10  (var1 = 10)
var1 =                 22              +   10  ;
var1 =                    32                   ;
32  (var1 = 32);
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Hi Andreas,thanks for helping.but i have a question.If the post-unary and pre-unary operators have the highest priority, why assign second var1 variable to 10 instead of 11? I did not understand the logic of assigning 10 – Surabaya Mar 25 '21 at 20:23
  • @darkblue You are confusing *order-of-evaluation* and *operator-precedence*. See my [other answer](https://stackoverflow.com/a/33121756/5221149) for a more complex example. Or see [this long explanation](https://stackoverflow.com/a/6801431/5221149) by Eric Lippert. – Andreas Mar 25 '21 at 20:25
  • thanks for helping. I saw your other answer and I understood to this part – Surabaya Mar 25 '21 at 20:37