- Home /
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:
What are the actual differences in both != vs !var?
And in what kind of situations should I use != & !var?
Thanks in advance!
Answer by save · Apr 09, 2012 at 09:42 PM
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.
! (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
Hi save!
Thanks for the explanation, I understand the difference better. But i still have some questions about it.
1) can the !(not) use in the != (not equals to)ifelse example that you gave? what i meant it's like
if(x+y = !z) ins$$anonymous$$d of if (x+y != z)
if(lastX == !transform.position.x) ins$$anonymous$$d of if(lastX != transform.position.x)
From my own understanding, is that, originally, != is checking if lastX is not equal to trasn.position.X it will execute something. This part, i understand very well from your example.
2) from 1st post on top, what is the logic error behind when i change it to if(lastX == !transform.position.x) ins$$anonymous$$d of if(lastX != transform.position.x)?
3) But if i changed it to if(var 1 == !var2), unity check if lastX is equal to NOT(trasn.position.X) then it will execute something too. Isn't same as "!=" ?
4) or it depends the situation that sometimes i can use the if(x+y = !z), and sometimes i cant?
I asked because I still couldn't understand the difference in both when using if else statement with checking variables
Thanks again!
As the answer says the "!" operand negates what comes after that. so "!true" becomes "false". When you do "if(a == !b)" first the value of b is negated, and a is then compared to that.The operand(afaik) only works on a variable that can have two values, true or false. "!3" doesn't really mean anything.
Thats the example why I couldn't understand the difference, (i came out with myself to explain to the logic why i can't understand it)
var is$$anonymous$$oving:boolean = true;
var hungry:boolean = false;
if (is$$anonymous$$oving =!hungry)
then John conti to move
same as
if (is$$anonymous$$oving != hungry)
then John conti to move
I'm aware that coding logic is fundamentally between != and not(var). but for effects of both, i need somemore help in understanding it.
Answer by by0log1c · Apr 10, 2012 at 06:58 AM
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)
@BY0LOG1C Thanks!
Because my expression is not a boolean, it's floating values thats why it doesn't work.
but if both var is boolean, do the 2 cases make any difference?
For instance,
var is$$anonymous$$oving:boolean = true;
var hungry:boolean = false;
if (is$$anonymous$$oving =!hungry)
then John conti to move
same as
if (is$$anonymous$$oving != hungry)
then John conti to move
in this case above, it don't make a difference right?
Thanks, I do understand != is fundamentally different from var1 = !var2 but i just can't understand the different of the effects
$$anonymous$$ostly, for readability, you wouldn't query such a condition as once this gets nested it really is a hard trail to follow when something blows up.
It should be if (is$$anonymous$$oving == !hungry)
which will return the same results as if (is$$anonymous$$oving != hungry)
.
You can try it out yourself with:
private var is$$anonymous$$oving : boolean = false;
private var hungry : boolean = false;
function Start () {
var returnDebug : String;
for (var i : int = 0; i < 8; i++) {
switch (i) {
case 0:
is$$anonymous$$oving = true; hungry = true;
returnDebug = "is$$anonymous$$oving == !hungry returns "+(is$$anonymous$$oving == !hungry).ToString()+" when is$$anonymous$$oving="+is$$anonymous$$oving.ToString()+" and hungry="+hungry.ToString();
break;
case 1:
is$$anonymous$$oving = false; hungry = true;
returnDebug = "is$$anonymous$$oving == !hungry returns "+(is$$anonymous$$oving == !hungry).ToString()+" when is$$anonymous$$oving="+is$$anonymous$$oving.ToString()+" and hungry="+hungry.ToString();
break;
case 2:
is$$anonymous$$oving = false; hungry = false;
returnDebug = "is$$anonymous$$oving == !hungry returns "+(is$$anonymous$$oving == !hungry).ToString()+" when is$$anonymous$$oving="+is$$anonymous$$oving.ToString()+" and hungry="+hungry.ToString();
break;
case 3:
is$$anonymous$$oving = true; hungry = false;
returnDebug = "is$$anonymous$$oving == !hungry returns "+(is$$anonymous$$oving == !hungry).ToString()+" when is$$anonymous$$oving="+is$$anonymous$$oving.ToString()+" and hungry="+hungry.ToString();
break;
case 4:
is$$anonymous$$oving = true; hungry = true;
returnDebug = "is$$anonymous$$oving != hungry returns "+(is$$anonymous$$oving != hungry).ToString()+" when is$$anonymous$$oving="+is$$anonymous$$oving.ToString()+" and hungry="+hungry.ToString();
break;
case 5:
is$$anonymous$$oving = false; hungry = true;
returnDebug = "is$$anonymous$$oving != hungry returns "+(is$$anonymous$$oving != hungry).ToString()+" when is$$anonymous$$oving="+is$$anonymous$$oving.ToString()+" and hungry="+hungry.ToString();
break;
case 6:
is$$anonymous$$oving = false; hungry = false;
returnDebug = "is$$anonymous$$oving != hungry returns "+(is$$anonymous$$oving != hungry).ToString()+" when is$$anonymous$$oving="+is$$anonymous$$oving.ToString()+" and hungry="+hungry.ToString();
break;
case 7:
is$$anonymous$$oving = true; hungry = false;
returnDebug = "is$$anonymous$$oving != hungry returns "+(is$$anonymous$$oving != hungry).ToString()+" when is$$anonymous$$oving="+is$$anonymous$$oving.ToString()+" and hungry="+hungry.ToString();
break;
}
Debug.Log(returnDebug);
}
}
Remember, ! negates, != compares.
Haha @BY0LOG1C ... reading that was almost kind of trippy ... :D
Your answer
Follow this Question
Related Questions
Distance sensor 1 Answer
How to write a "or "statement & a if Else with or condition? 2 Answers
Conversion method types(void to bool)[SOLVED] 1 Answer