我=我++不增加 i。为什么?

发布于 2024-11-24 23:34:09 字数 717 浏览 0 评论 0原文

可能的重复:
为什么会进入无限循环?

之类的东西code>i = i++ 在 C 和 C++ 中具有未定义的行为,因为标量对象的值在同一表达式内更改两次,而无需插入序列点。

不过,我认为这些类型的表达式在 C# 或 Java 中具有明确定义的行为,因为据我所知,参数的求值是从左到右的,并且到处都有序列点。

也就是说,我希望 i = i++ 等同于 i++。但事实并非如此。以下程序输出 0

using System;
class Program
{
    static void Main(string[] args)
    {
        int i = 0;
        i = i++;
        Console.WriteLine(i);
    }
}

你能帮我理解为什么吗?

免责声明: 我完全意识到,无论是否定义了上述构造的行为,它们都是愚蠢的、无用的、不可读的、不必要的,并且不应该在代码中使用。我只是好奇。

Possible Duplicates:
Why does this go into an infinite loop?

Things like i = i++ have undefined behavior in C and C++ because the value of a scalar object is changes twice within the same expression without intervening sequence point.

However I suppose that these kind of expressions have well-defined behavior in C# or Java because AFAIK the evaluation of argument goes left to right and there are sequence points all over.

That said, I'd expect i = i++ to be equivalent to i++. But it's not. The following program outputs 0.

using System;
class Program
{
    static void Main(string[] args)
    {
        int i = 0;
        i = i++;
        Console.WriteLine(i);
    }
}

Could you help me understand why?

Disclaimer:
I am fully aware that whether or not the behavior of above-mentioned constructs is defined, they are silly, useless, unreadable, unnecessary and should not be used in code. I am just curious.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北座城市 2024-12-01 23:34:09

该行为在 C# 中定义良好,计算顺序为:

  1. 左侧 i 计算为变量 i
  2. 右侧计算为 0,并且 i code> 递增(现在 i==1
  3. 执行赋值,将 i 设置为 0。(现在 i==0

最终结果是i==0

通常,您首先创建一个表达式树。要评估它,您首先评估左侧,然后评估右侧,最后评估根部的操作。递归地这样做。

The behavior is well defined in C# and the evaluation order is:

  1. Left side i is evaluated to the variable i
  2. Right side is evaluated to 0, and i is incremented (now i==1)
  3. The assignment is executed, it sets i to 0. (now i==0)

The end result is i==0.

In general you first create an expression tree. To evaluate it you evaluate first the left side, then the right side and finally the operation at the root. Do that recursively.

╰ゝ天使的微笑 2024-12-01 23:34:09

后置自增表达式i++的结果是i的原始值。因此,在对 i++ 求值后(递增 i),您将 i 的旧值分配给 ... i >。

只需使用

i++;

;)

The result of the post-increment expression i++ is the original value of i. So after i++ has been evaluated (incrementing i), you assign the old value of i to ... i.

Just use

i++;

;)

梦在深巷 2024-12-01 23:34:09

i = ++i 是执行您认为此处发生的操作的代码。 i++ 实际上的行为有点不同。

使用i++i的值增加了,但是i++的值并不是i的新值,它是之前的值。因此,当您执行 i = i++ 时,您是在说“增加 i 的值,然后将 i 设置为旧值”。

i = ++iis the code that does what you think is going on here. i++ actually behaves a bit differently.

With i++, the value of i is increased, but the value of i++ is not the new value of i, it's the previous value. So when you do i = i++, you're saying "increase the value of i, then set i to the old value".

情场扛把子 2024-12-01 23:34:09

那么,在进行赋值之前必须先计算右侧表达式。现在,i++ 将计算出 i 的当前值,并且 i 的值随后将增加 1。但是,赋值尚未执行,当执行时,它将用 rhs 表达式的计算结果覆盖 i (1) 的当前值,在您的情况下为 0

。主要区别在于 ++i(预增量,其计算结果为 i after 增量的值) 和 i++,或者后增量,其计算结果为增量之前i的当前值。

如果您改用 ++i,则右侧表达式的计算结果将为 1,导致 i == 1。

Well, the right-hand side expression must be evaluated before the assignment can take place. Now, i++ will evaluate to the current value of i, and i's value will subsequently increase by one. However, the assignment hasn't been performed yet, and when it is, it will overwrite the current value of i (1) with whatever the rhs expression evaluated to, which in your case was 0.

The key difference is between ++i (pre-increment, which evaluates to the new value of i after incrementing) and i++, or post-increment, which evaluates to the current value of i before incrementing.

Had you used ++i instead, the right-hand side expression would have evaluated to 1, resulting in i == 1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文