Incrementor logic

后端 未结 7 2170
醉梦人生
醉梦人生 2020-11-27 05:37

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         


        
7条回答
  •  攒了一身酷
    2020-11-27 05:50

    Because of the highest precedence (...) will be evaluated first then ++ & -- and then remaining operators. Your expression is like

    i = i += ( (++i) + (i+=2 + (--i)) - (++i) );
    
    //i = i = i + ( (++i) + (i+=2 + (--i)) - (++i) );
    //i = i = 0 + ( (++i) + (i+=2 + (--i)) - (++i) );
    //i = i = 0 + ( (1) + (i+=2 + (--i)) - (++i) );
    //i = i = 0 + ( (1) + (i+=2 + (0)) - (++i) );
    //i = i = 0 + ( (1) + (2 + (0)) - (++i) );
    //i = i = 0 + ( (1) + (2) - (++i) );
    //i = i = 0 + ( (1) + (2) - (3) );
    //i = i = 0 + ( 3 - 3 );
    //i = i = 0 + ( 0 );
    //i = 0
    

    Please fully parenthesize your expression. You will understand evaluation order of expression.

    For your EDIT 1

    i = i+=( (++i) + (i+=32500 + (--i) ) - (++i) ); 
    
    // 0 + ( (++i) + (i+=32500 + (--i) ) - (++i) ); // i = 0
    // 0 + ( (1) + (i+=32500 + (--i) ) - (++i) );  // i = 1
    // 0 + ( (1) + (i+=32500 + (0) ) - (++i) ); // i = 0
    // 0 + ( (1) + (32500 + (0) ) - (++i) );  // i = 32500
    // 0 + ( (1) + (32500) - (++i) ); // i = 32500
    // 0 + ( (1) + (32500) - (32501) ); // i = 32501
    // 0 + ( 32501 - 32501 ); // i = 32501
    // 0 // i = 0
    

提交回复
热议问题