简体   繁体   English

这个运算符是什么意思? / =

[英]What does this operator mean? /=

I have just come across some code with the line: 我刚刚遇到了一些代码:

n /= 10;

I assumed it was a typo and removed the / to make it n = 10 , but the program no longer works. 我认为这是一个错字,并删除了/使其变为n = 10 ,但是该程序不再起作用。

Never seen this sort of operator before, anybody know? 有人以前从未见过这种操作员吗?

The /= is a shorthand operator. /=是简写运算符。

a /= b

is equivalent to 相当于

c = a/b;
a = c;

n /= 10 is taking the value of n , dividing it by 10 and reassigning that value to n . n /= 10n的值除以10然后将该值重新分配给n

It's just shorthand for n = (n / 10) just like n++ is n = n + 1 . 它只是n = (n / 10)简写,就像n++n = n + 1

It is the same as 与...相同

n = n/10 ;

You can use this form also with other operators (+, -, %,...). 您也可以将其与其他运算符(+,-,%,...)一起使用。

The case n+=1 (n-=1) can also be written as n++ (increase n by 1) or ++n . n + = 1(n- = 1)的情况也可以写成n++ (将n增加1)或++n

The /= is one of the shorthand operators. /=是速记运算符之一。

A shorthand operator is a concise way to express something that is already available in a programming language. 速记运算符是表达某种已在编程语言中提供的内容的简洁方法。

They are: 他们是:

  • += (Eg: x += 4; is equivalent to x = x + 4; ) += (例如: x += 4;等效于x = x + 4;
  • -= (Eg: x -= 4; is equivalent to x = x - 4; ) -= (例如: x -= 4;等效于x = x - 4;
  • *= (Eg: x *= 4; is equivalent to x = x * 4; ) *= (例如: x *= 4;等效于x = x * 4;
  • /= (Eg: x *= 4; is equivalent to x = x / 4; ) /= (例如: x *= 4;等于x = x / 4;
  • %= (Eg: x %= 4; is equivalent to x = x % 4; ) %= (例如: x %= 4;等于x = x % 4;

It simply means n = n/10 . 它只是意味着n = n/10 The same form can be used with other operators too. 同样的形式也可以与其他运算符一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM