I want to check whether a value is equal to 1. Is there any difference in the following lines of code
x == 1
1 == x
… in terms of the compiler execution?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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:
Instead you could force a compiling error instead of a bug:
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.