This is just out of curiosity I ask...
99% of the code I see anywhere, when using "IF", will be of the format "If (RValue == LValue) ...". Example:
If (variableABC == "Hello World") ...
There are other examples where I see the opposite:
If ("Hello World" == variableABC)
Anyone know how this started and why it's done?
It is done because of this mistake in C and C++:
if (variableABC = "Hello World") ...
^
(Watch here)
This way we have a compilation error:
if ("Hello World" = variableABC)
^
(Watch here)
For example, C# and Java languages don't need this trick.
The latter is done to prevent unintended assignments, if you, by mistake, use the assignment operator =
instead of the equality operator ==
'
Some languages do not allow an assignment in an if-condition, in which case either is fine.
In languages that do accept assignments in if conditions, I always prefer going with the latter case.
This is done because of the errors developers often do writing = instead of ==. In C++ integers can be treated as booleans and you got no errors at compile time.
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