“x = x++"之后的 x 是什么? [英] What is x after "x = x++"?

查看:35
本文介绍了“x = x++"之后的 x 是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行此操作时(幕后)会发生什么?

What happens (behind the curtains) when this is executed?

int x = 7;
x = x++;

也就是说,当一个变量在一个语句中被后递增并赋值给它自己时?我编译并执行了这个.x 仍然是 7 即使在整个语句之后.在我的书中,它说 x 是递增的!

That is, when a variable is post incremented and assigned to itself in one statement? I compiled and executed this. x is still 7 even after the entire statement. In my book, it says that x is incremented!

推荐答案

x 确实增加了.但是您正在将 x 的旧值分配回自身.

x does get incremented. But you are assigning the old value of x back into itself.

x = x++;

  1. x++ 递增 x 并返回其旧值.
  2. x = 将旧值分配回自身.
  1. x++ increments x and returns its old value.
  2. x = assigns the old value back to itself.

所以最后,x 被赋值回它的初始值.

So in the end, x gets assigned back to its initial value.

这篇关于“x = x++"之后的 x 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆