# why != is different from var1 = !var2?

Hi Guys,

I’m learning the unity from the Unity 3.x game development by example. I would like to know why != is not same as var1 == !var2 & !var1 == var2

Though, I tried simple coding like if(xyz != abc), if(xyz == !abc), it looks the same to me but actual application is a different story.

I tried change the code below:

``````var lastX:float; // this will stoe the last position of the character
var isMoving:boolean = false; // flags whether or not the player is in motion

function Start ()
{
animation.Stop (); // this will stops unity from playing character's default animation.
}

function Update ()
{
var halfW:float = Screen.width /2 ;
transform.position.x = (Input.mousePosition.x)/20;
if(lastX != transform.position.x)
{
//x values between this update cycle and the last one
//arent the same!
// that means the player is moving the mouse
if(!isMoving)
{
// the player was standing still
// lets flag him to "isMoving"
isMoving = true;
animation.Play("step");

}
}
else
{
// the player's x position is the same as this update cycle
// as it was the last! the player has stopped moving the mouse
if(isMoving)
{
// the player has stopped moving, so lets update the flag
isMoving = false;
animation.Play("idle");

}
}

lastX = transform.position.x;
}
``````

I realize that if I change the code,
`if(lastX != transform.position.x)` to
`if(lastX = !transform.position.x)` or `if(!lastX = transform.position.x)`.
The character won’t move as expected.

So my questions are:

1. What are the actual differences in
both != vs !var?
2. And in what kind of
situations should I use != & !var?

1. The difference between them are that ! is a logical negation operator which negates its operand and != is an inequality operator which returns true when its operand aren’t equal. They are both used in boolean operations but should be treated separately. ! is used for negating, != is used for comparison.

2. ! (not) can be used in this way:

if (!booleanValue) //If booleanValue is false

booleanValue = !booleanValue //Set booleanValue to not be equal to booleanValue (switches between true and false)

!= (not equals to) can be used in this way:

if (x+y != z) //If x+y isn’t equal to z

if (“apple” != “banana”) //Should return true

This might not be worthy of an answer, turns out it’d be easier for me to just write plenty of example and hope you deduct what does what.

``````(true) == true
(!true) == false
(true == true) == true
(true == false) == false
(true != true) == false
(true != false) == true
(!true == false) == true
(false == !false) == false
(!true != !true) == false
(!true != false) == false
(!false == !false) == true
(5 != 5) == false
(5 == 5) == true
!(5 == 5) == false
(5 == !5) == error, the *!* operator may only be used on boolean expression.

(((true == !false) != !(false != false)) == true)
(((true) != !(false)) == true)
((true != true) == true)
(false == true)
(false)
``````