赋值给变量没有效果吗? [英] The assignment to variable has no effect?

查看:1001
本文介绍了赋值给变量没有效果吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我这样做时: 计数= ++计数; 为什么会收到警告-分配给变量计数无效? 这意味着计数是递增的,然后分配给它自己或其他? 和++ count一样吗? count = count ++会发生什么? ?为什么我没有收到警告?

When I do this: count = ++count; Why do i get the warning - The assignment to variable count has no effect ? This means that count is incremented and then assigned to itself or something else ? Is it the same as just ++count ? What happens in count = count++; ? Why don't I get a warning for this ?

推荐答案

count++++count都是count=count+1的缩写.分配是内置的,因此没有必要再次分配它. count++(也称为后缀)和++count(也称为前缀)之间的区别在于,++count会在行的其余部分之前出现,和count++将在该行的其余部分之后发生.

count++ and ++count are both short for count=count+1. The assignment is built in, so there's no point to assigning it again. The difference between count++ (also knows as postfix) and ++count (also known as prefix) is that ++count will happen before the rest of the line, and count++ will happen after the rest of the line.

如果要拆开count=count++,您将得到以下结果:

If you were to take apart count=count++, you would end up with this:

    count = count;
    count = count+1;

现在,您可以了解为什么后缀不给您警告:末尾实际上已更改了一些内容.

Now you can see why postfix won't give you a warning: something is actually being changed at the end.

如果拆开count=++count,您将得到以下结果:

If you take apart count=++count, you would end up with this:

    count = count+1;
    count = count;

如您所见,第二行代码无用,这就是编译器警告您的原因.

As you can see, the second line of code is useless, and that's why the compiler is warning you.

这篇关于赋值给变量没有效果吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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