Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

== Operator and operands

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

like image 970
rahul Avatar asked Mar 24 '09 13:03

rahul


People also ask

What is this == operator called?

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.

What is difference operator and operand?

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.

What is the meaning of == in C?

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.


1 Answers

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.

like image 56
Brian R. Bondy Avatar answered Jan 03 '23 16:01

Brian R. Bondy