Pre/post increment/decrement and operator order confusion

前端 未结 4 1399
天命终不由人
天命终不由人 2021-01-15 07:45

I was going through some exercises but I am confused in this one:

public static int f (int x, int y) {
  int b=y--;
  while (b>0) {
    if (x%2!=0)  {
            


        
4条回答
  •  一整个雨季
    2021-01-15 08:33

    int b=y--; first assignes b=y and then decrements y (y--).

    Also take a look at the prefix/postfix unary increment operator.

    This example (taken from the linked page) demonstrates it:

    class PrePostDemo {
        public static void main(String[] args){
            int i = 3;
            i++;
            // prints 4
            System.out.println(i);
            ++i;               
            // prints 5
            System.out.println(i);
            // prints 6
            System.out.println(++i);
            // prints 6
            System.out.println(i++);
            // prints 7
            System.out.println(i);
        }
    }
    

提交回复
热议问题