I want to check whether a value is equal to 1. Is there any difference in the following lines of code
Evaluated value == 1
1 == evaluated value
in terms of the compiler execution
Relational Operators == (Equal to)– This operator is used to check if both operands are equal. != (Not equal to)– Can check if both operands are not equal.
The operators indicate what action or operation to perform. The operands indicate what items to apply the action to. An operand can be any of the following kinds of data items: Constant. Variable.
The '==' operator checks whether the two given operands are equal or not. If so, it returns true. Otherwise it returns false. For example: 5==5 This will return true.
In most languages it's the same thing.
People often do 1 == evaluated value because 1 is not an lvalue. Meaning that you can't accidentally do an assignment.
Example:
if(x = 6)//bug, but no compiling error
{
}
Instead you could force a compiling error instead of a bug:
if(6 = x)//compiling error
{
}
Now if x is not of int type, and you're using something like C++, then the user could have created an operator==(int) override which takes this question to a new meaning. The 6 == x wouldn't compile in that case but the x == 6 would.
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