简体   繁体   English

i == 0和0 == i之间有区别吗?

[英]Is there a difference between i==0 and 0==i?

First code: 第一个代码:

  if(i==0) {// do instructions here}

Second code: 第二个代码:

  if(0==i) { // do instructions here }

What is the difference between the blocks? 块之间有什么区别?

Functionally, there is no difference. 在功能上,没有区别。
Some developers prefer writing the second format to avoid assignment typos(in case you miss a = ), so that compiler warns of the typo. 一些开发人员更喜欢编写第二种格式以避免分配拼写错误(如果你错过了= ),编译器会警告输入错误。
The second is famously known as Yoda Condition . 第二个是着名的尤达条件

尤达条件

I say there is no difference because, you cannot guard yourself against every minuscule detail and rely on compiler to cry out aloud for you.If you intend to write a == you should expect yourself to write a == and not a = . 我说没有区别因为,你无法防范每一个微不足道的细节,依靠编译器为你大声喊叫。如果你打算写一个==你应该期望自己写一个==而不是a =
Using the second format just leads to some obscure non-readable code. 使用第二种格式只会导致一些模糊的不可读代码。
Also, most of the mainstream compilers warn of the assignment instead of equality typo by emitting an warning once you enable all the warnings(which you should anyways). 此外,大多数的主流编译器通过警告的分配 ,而不是平等错字的发射警告 ,一旦你让所有的警告(你应该反正)。

The second version is supposed to be safer. 第二个版本应该更安全。

In case you forget one equal sign, it does not change the value of i to zero. 如果您忘记了一个等号,它不会将i的值更改为零。

Functionally, they are the same in C; 在功能上,它们在C中是相同的; I'm not sure about other languages where ugly things like operator overloading come into play. 我不确定运算符重载等丑陋的东西会发挥作用的其他语言。

Stylistically, the latter is extremely counter-intuitive and personally I find it extremely ugly. 在风格上,后者非常反直觉,我个人觉得它非常难看。 The point is to get the compiler to throw an error when you accidentally write = instead of == , but good compilers have an option to warn you about this anyway so it's unnecessary. 关键是当你不小心写=而不是==时让编译器抛出一个错误,但好的编译器有一个选项来警告你这个,所以这是不必要的。

Yep they are same as far as C# is concerned. 是的,就C#而言,它们是相同的。 For more complex situations visit A==B vs B==A, What are the differences 对于更复杂的情况,请访问A == B vs B == A,有什么区别

For C++, it's possible, though unlikely, that there could be a difference. 对于C ++,虽然不太可能,但可能存在差异。 It depends upon what i's type is. 这取决于我的类型。 eg 例如

struct Foo
{
    int x;
};

bool operator==(Foo lhs, int rhs)
{
    return lhs.x == rhs;
}

bool operator==(int lhs, Foo rhs)
{
    std::cout << "Hi!";
    return true;
}

Someone who writes code like that should of course be shot. 编写类似代码的人当然应该被枪杀。

When you write (0==i) the error of using single equal to sign by mistake (eg ) if ( i = 0) is eliminated. 当你写(0 == i)时,错误地使用单个等于符号的错误(例如)if if(i = 0)被消除。 Nothing else. 没有其他的。

no difference, some people prefer the second one to catch the common mistake of doing assignment ( = ) instead of equality test ( == ) 没有区别,有些人更喜欢第二个捕捉做分配( = )而不是等式测试( == )的常见错误

0 = i would fail at compilation 0 = i在编译时会失败

In C# there is no difference. 在C#中没有区别。 However in C++ there was a difference in performance which is why you see both used in C# code these days - actually I'm thinking of i++ vs ++i about performance - 0 == i is a common coding recommendation in C/C++ to avoid i = 0 as an accidental operation 但是在C ++中,性能存在差异,这就是为什么你们现在看到C#代码都使用了 - 实际上我正在考虑i ++ vs ++ i关于性能 - 0 ==我是C / C ++中常见的编码推荐避免i = 0作为意外操作

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

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