If (Rvalue == LValue)

It's just out of curiosity, I ask ...

99% of the code that I see anywhere, when using "IF", will be in the format "If (RValue == LValue) ...". Example:

If (variableABC == "Hello World") ... 

There are other examples when I see the opposite:

 If ("Hello World" == variableABC) 

Does anyone know how this started and why it was made?

+6
source share
3 answers

This is done due to this error in C and C ++:

 if (variableABC = "Hello World") ... ^ (Watch here) 

Thus, we have a compilation error:

 if ("Hello World" = variableABC) ^ (Watch here) 

For example, C # and Java do not need this trick.

+3
source

The latter is done to prevent unintentional assignments if you mistakenly use the assignment operator = instead of the equality operator == '

Some languages ​​do not allow assignment in an if condition, in which case this is normal.

In languages ​​that accept assignments under if conditions, I always prefer to go in the latter case.

+2
source

This is due to errors that developers often make write = instead of ==. In C ++, integers can be treated as logical and you did not get errors at compile time.

+1
source

Source: https://habr.com/ru/post/913745/


All Articles