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 =
.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With