c语言增减量运算符和赋值_++ i和i ++增减运算符

c语言增减量运算符和赋值

These increment operators and the corresponding decrement operators are perhaps a little confusing to anyone who hasn't come across them before.

这些增量运算符和相应的减量运算符可能会使以前从未遇到过的任何人感到困惑。

To start with there are several different ways to add or subtract one.

首先,有几种不同的方法可以相加或相减。


i += 1;
我+ = 1;
i++;
i ++;
++i;
++ i;

For subtracting one there are the same four choices with - substituted for each +.

要减去一个,有相同的四个选择,每个-都用-代替。

So why exactly does JavaScript (and other languages) provide so many different ways to do the same thing? Well, for one thing, some of these alternatives are shorter than others and so involve less typing. Using += easily allows any number and not just one to be added to a variable without having to enter the variable name twice.

那么,为什么JavaScript (和其他语言)确实提供了这么多种不同的方式来做同一件事呢? 好吧,一方面,这些替代方案中的一些替代方案比其他替代方案更短,因此涉及的打字次数更少。 使用+ =可以轻松地将任意数字而不只是一个数字添加到变量中,而不必两次输入变量名称。

That still doesn't explain why both i++ and ++i exist since both can only be used to add one and both are the same length. The reason for the two alternatives is that these are not really intended to be used as stand alone statements but are really designed to be able to be incorporated into more complex statements where you actually update more than one variable in the one statement.statements where you actually update more than one variable in the one statement.

但这仍不能解释为什么i ++和++ i都存在,因为两者都只能用于加一,并且两者的长度相同。 这两种选择的原因是,它们并不是真正用作独立的语句,而实际上是为了能够合并到更复杂的语句中而在其中,您实际上在一个语句中更新了多个变量。 语句 ,你实际上是在一个声明中更新多个变量。

Probably the simplest such statement is as follows:

可能最简单的此类语句如下:

j = i++;
j = i ++;

This statement updates the values of both of the variables i and j in the one statement. The thing is that while ++i and i++ do the same thing as far as updating i is concerned they do different things with regard to updating other variables. The above statement can be written as two separate statements like this:

该语句更新一个语句中变量i和j的值。 事实是,就更新i而言,虽然++ i和i ++做相同的事情,但它们在更新其他变量方面却做不同的事情。 上面的语句可以这样写成两个单独的语句:


i += 1;
我+ = 1;

Note that combining them together means we have eight characters instead of 13. Of course, the longer version is much clearer where it comes to working out what value j will have.

请注意,将它们组合在一起意味着我们有8个字符而不是13个字符。当然,较长的版本在计算j的值时会更清楚。

Now if we look at the alternative:

现在,我们来看一下替代方案:

j = ++i;
j = ++ i;

This statement is the equivalent of the following:

该语句等效于以下内容:


j = i;
j = i;

This, of course, means that j now has a different value to what it had in the first example. The position of the ++ either before or after the variable name controls whether the variable gets incremented before or after it gets used in the statement that it is used in.

当然,这意味着j现在的值与第一个示例中的值不同。 变量名之前或之后的++位置控制变量在使用它的语句中使用之前还是之后增加。

Exactly the same applies when you consider the difference between --i and i-- where the position of the -- determines whether one is subtracted before or after the value is used.

当考虑--i和i--之间的差异时,情况完全相同,其中-的位置确定在使用该值之前还是之后减去一个。

So when you use it separately as a single statement it makes no difference whether you place it before or after the variable name (except for a microscopic speed difference that no one will ever notice). It is only once you combine it with another statement that it makes a difference to the value that gets assigned to some other variable or variables.

因此,当您将其单独用作单个语句时,将其放在变量名之前还是之后都没有区别(除了微小的速度差异,没人会注意到)。 只有将其与另一条语句组合后,它才会对分配给某个或多个其他变量的值产生影响。

翻译自: https://www.thoughtco.com/andandi-and-iandand-2037549

c语言增减量运算符和赋值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值