Rafik Bari
Rafik Bari

Reputation: 5037

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?

Upvotes: 9

Views: 1909

Answers (8)

Alok Save
Alok Save

Reputation: 206566

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.

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 =.
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).

Upvotes: 32

Benjamin Lindley
Benjamin Lindley

Reputation: 103733

For C++, it's possible, though unlikely, that there could be a difference. It depends upon what i's type is. e.g.

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.

Upvotes: 3

Lav
Lav

Reputation: 1896

Yep they are same as far as C# is concerned. For more complex situations visit A==B vs B==A, What are the differences

Upvotes: 2

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215367

Functionally, they are the same in 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.

Upvotes: 5

Pierre Lacave
Pierre Lacave

Reputation: 2690

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

Upvotes: 1

vrk001
vrk001

Reputation: 335

When you write (0==i) the error of using single equal to sign by mistake (e.g. ) if ( i = 0) is eliminated. Nothing else.

Upvotes: 1

Richard Blewett
Richard Blewett

Reputation: 6109

In C# there is no difference. 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

Upvotes: 0

Beno&#238;t
Beno&#238;t

Reputation: 16994

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.

Upvotes: 7

Related Questions