9

First code:

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

Second code:

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

What is the difference between the blocks?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rafik Bari
  • 4,867
  • 18
  • 73
  • 123

8 Answers8

32

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

Alok Save
  • 202,538
  • 53
  • 430
  • 533
7

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.

Benoît
  • 16,798
  • 8
  • 46
  • 66
5

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.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • 1
    Operator overloading is beautiful! (When applied correctly.) That's one of the things I love about C++, again and again. – leemes May 29 '12 at 00:19
3

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.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
2

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

Community
  • 1
  • 1
Lav
  • 1,850
  • 15
  • 17
1

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

vrk001
  • 355
  • 1
  • 2
  • 10
1

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

Pierre Lacave
  • 2,608
  • 2
  • 19
  • 28
0

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

Richard Blewett
  • 6,089
  • 1
  • 18
  • 23