[英]++ — Operator precedence puzzle
For the long time I thought I get it, and I was going to create some puzzles to learn some of my „students“ on the topic of operator precedence in c#. 长期以来,我一直以为自己会明白,因此我将创建一些难题,以学习一些有关C#中运算符优先级的“学生”。 But it came out that I still don't get it right. 但是结果是我仍然做得不好。
Puzzles: What's the output here? 困惑:这里的输出是什么?
int a = 0;
int x = --a + a++;
Console.WriteLine(x);
Console.WriteLine(a);
Output: 输出:
-2 0 -2 0
All clear here, I expected this 一切都清楚了,我期望这个
Next, the problem one: 接下来,问题一:
int b = 0;
int y = b-- + b++;
Console.WriteLine(y);
Console.WriteLine(b);
Output: 输出:
-1 0 -1 0
Well, here I also expected y to be -2… Now I'm trying to apply operator precedence rules and order of evaluation, and not sure I explained it to myself. 好吧,在这里我也期望y为-2 ...现在我试图应用运算符优先级规则和求值顺序,并且不确定我向自己解释了它。 Read this post again few times today, but still don't quite get it why is the result here -1? 今天再读几次该帖子 ,但仍然不太明白为什么这里的结果为-1? Can someone help with how is the second result evaluated. 有人可以帮助评估第二个结果如何。 why and how is it different than the first one? 为什么和第一个有什么不同?
b--
is post-decrement. b--
递减。 So: 所以:
b-- returns zero and subtracts 1 from b, leaving -1 in b.
b++ returns the -1 from the last step and adds 1, leaving 0 in b.
Final result of the addition: -1. 加法的最终结果:-1。
Do what the compiler does: break it down slowly and surely into equivalent programs. 做编译器要做的事情:将它缓慢而确定地分解为等效程序。
int b = 0;
int y = b-- + b++;
is equivalent to 相当于
int b, y;
b = 0;
y = b-- + b++;
is equivalent to 相当于
int b, y;
b = 0;
int leftAddend = b--;
int rightAddend = b++;
y = leftAddend + rightAddend;
is equivalent to 相当于
int b, y;
b = 0;
int originalb1 = b;
int newb1 = originalb1 - 1;
b = newb1;
int leftAddend = originalb1;
int originalb2 = b;
int newb2 = originalb2 + 1;
b = newb2;
int rightAddend = newb2;
y = leftAddend + rightAddend;
And now annotate each with its value: 现在用其值注释每个值:
int b, y;
b = 0; // b is now 0
int originalb1 = b; // originalb1 is now 0
int newb1 = originalb1 - 1; // newb1 is now -1
b = newb1; // b is now -1
int leftAddend = originalb1; // leftAddend is now 0
int originalb2 = b; // originalb2 is now -1
int newb2 = originalb2 + 1; // newb2 is now 0
b = newb2; // b is now 0
int rightAddend = originalb2;// rightAddend is now -1
y = leftAddend + rightAddend;// y is now -1
This is precisely how the compiler deals with this situation; 这正是编译器处理这种情况的方式。 the compiler is just a bit more clever about optimizing away the temporaries. 编译器在优化临时变量方面更加聪明。 Analyzing expressions gets easy if you just break it down into simpler steps . 如果将表达式分解为更简单的步骤,则分析表达式将变得很容易。
int b = 0;
int y = b-- + b++;
Break it down by step: 逐步分解:
y = b--
Here, y is set to b (0), then b is decremented to -1
. 在此,将y设置为b(0),然后将b减为-1
。
+ b++
Here, y (0) is added to b (decremented to -1 in prev step) equaling -1
, then b is incremented to zero. 在此,将y(0)加到等于-1
b(在上一步中减为-1),然后b递增为零。 Output: 输出:
-1 0
Postfix --
/ ++
returns the original value of the variable. 后缀--
/ ++
返回变量的初始值。 So: 所以:
With your example b-- + b++
: 以您的示例b-- + b++
:
b--
means decrement b, return the original value. b--
表示递减b,返回原始值。 So b = b - 1
, b
is now -1
, and the value of the expression is 0
. 因此b = b - 1
, b
现在为-1
,表达式的值为0
。 b++
means increment b, return the original value. b++
表示递增b,返回原始值。 So b = b + 1
, b
is now 0
, and the value of the expression is -1
. 因此b = b + 1
, b
现在为0
,表达式的值为-1
。 0 + -1 == -1
. 然后, 0 + -1 == -1
。 This is y
. 这是y
。 b
is still 0
. b
仍为0
。 This question has been answered, but I wanted to state the answer another way. 这个问题已经回答了,但是我想用另一种方式陈述答案。
int b = 0;
int y = b-- + b++;
The expression b--
evaluates to 0
(the original value of b) and has the side effect (applied after evaluation) of decrementing b to -1. 表达式b--
求值为0
(b的原始值),并且具有将b递减为-1的副作用(在求值后应用)。
The expression b++
evaluates to -1
(because of the previous side effect) and has the side effect (applied after evaluation) of incrementing b to 0. 表达式b++
计算结果为-1
(由于先前的副作用),并且具有将b增加到0的副作用(在评估后应用)。
This leaves the expression 0 + -1
which is -1
. 剩下的表达式0 + -1
为-1
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.