简体   繁体   English

前/后增量/减量和运算符顺序混淆

[英]Pre/post increment/decrement and operator order confusion

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)  {
      --x;
      y=y-2; 
    }
    else { 
      x=x/2;
      b=b-x-1; 
    }
  }
  return x+y; 
} 

What is the purpose of b=y-- ? b=y--的目的是什么? So, for example, x=5 and y=5 when we first go inside of while loop ( while (b>0) ) will b = 4 or 5?因此,例如,当我们第一次进入 while 循环( while (b>0) )时, x=5y=5 b = 4 还是 5? When I am running the code in my computer b is 5. And the return is 3. It is really unclear to me.当我在我的计算机上运行代码时, b是 5。返回值是 3。我真的不清楚。 Sorry if I am unclear in my question.对不起,如果我的问题不清楚。

int b=y--; first assignes b=y and then decrements y ( y-- ).首先分配b=y然后递减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);
    }
}

The difference between a post-increment/decrement and a pre-increment/decrement is in the evaluation of the expression .后递增/递减和预增量/减量之间的差异是在 表达的评价

The pre-increment and pre-decrement operators increment (or decrement) their operand by 1, and the value of the expression is the resulting incremented (or decremented) value. pre-increment 和 pre-decrement 运算符将它们的操作数增加(或减少)1,表达式的值是结果增加(或减少)的值。 In contrast, the post-increment and post-decrement operators increase (or decrease) the value of their operand by 1, but the value of the expression is the operand's original value prior to the increment (or decrement) operation.相比之下,后递增和后递减运算符将其操作数的值增加(或减少)1,但表达式的值是操作数在递增(或递减)操作之前的原始值。

In other words:换句话说:

int a = 5;
int b;
b = --a;    // the value of the expression --a is a-1. b is now 4, as is a.
b = a--;    // the value of the expression a-- is a. b is still 4, but a is 3.

Remember that a program must evaluate expressions to do everything .请记住,程序必须对表达式求值才能完成所有操作 Everything is an expression, even just a casual mention of a variable.一切都是一个表达式,即使只是随便提到一个变量。 All of the following are expressions:以下都是表达式:

  • a
  • a-1
  • --a && ++a
  • System.out.println(a)

Of course, in the evaluation of expressions, operator precedence dictates the value of an expression just as the PEMDAS you learned in grade school.当然,在表达式的计算中, 运算符优先级决定了表达式的值,就像你在小学学到的PEMDAS 一样 Some operators, such as increment/decrement, have side effects, which is of course great fun, and one of the reasons why functional programming was created.一些操作符,比如增量/减量,有副作用,这当然很好玩,也是创建函数式编程的原因之一。

I believe b would equal 5 entering the loop because我相信 b 等于 5 进入循环,因为

b=y--;

When the "--" is behind the variable it decrements it after the action.当“--”在变量后面时,它会在操作后递减它。

It's poor coding, as it can confuse new programmers.这是糟糕的编码,因为它会使新程序员感到困惑。

The function, assuming it is passing by value, like in the example above (as opposed to passing by reference) takes a copy of y , decrements it, and assigns it to b .该函数假设它是按值传递,就像上面的例子(而不是按引用传递)获取y的副本,将其递减,并将其分配给b It does not alter the argument passed to the function when it was called.它不会改变在调用函数时传递给函数的参数。

Post increment岗位增量

x++; 
x += 1; 

Post decrement递减

x--; 
x -=1; 

Pre increment : ++x;预增量: ++x;
Pre decrement : --x;预减:-- --x;

According to the Head First Java:根据 Head First Java 的说法:

Difference between x++ and ++x : x++++x之间的区别:

int x = 0; int z = ++x; 
Produces: x is 1, x is 1
in x = 0; int z = x++; 
Produces: x is 1, z is 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM