Difference Between = and ==
TurnipTruck
Posts: 1,485
Greetings,
I am examining some code. What is the difference between = and ==
Thanks.
I am examining some code. What is the difference between = and ==
Thanks.
0
Comments
Correct me if I'm wrong but this is how I've always viewed it:
= is for assignment generally, but can be used in a relational expression also
For example, either of the following lines should work fine:
intMyInt = 1
IF(intMyInt = 1) DoFunction()
== is relational only (boolean expressions)
For example:
IF(intMyInt == 1) DoFunction()
I don't really use the == but it can make the code look better and it is easier to pick out relational expressions since it goes right along with && (AND), || (OR), etc.
--Eric
X = 3 <-- assign the value of 3 to the variable called X
IF (X = 3) <-- comparison - is one value equal to another?
== is found in more common programming languages, and to the best of my knowledge is simply a "comparison only" function.
IF (X == 3) <-- same as above
X == 3 <-- not an assignment operator, so this will fail to compile
In C, = does assignment only, and using it the way we're used to in Netlinx/Axcess for comparison will cause problems...
- Chip
P.S. Of course, some days I feel nostalgic and miss Pascal syntax, where := is the assignment operator.
As a C/C++ programmer, the netlinx compiler allows my normal usage of both to compile just fine.
This is a tad off topic but you can also drop the = sign(s) for comparison and do something like:
IF(x) //will drop thru if x is anything other than 0.
I don?t know if others consider that proper coding or not.
Though, when comparing multiple things that need to be zero in order for the IF statement to be true, I typically spell it out like so.... Rather than saying: Though both are identical IF statements. The use of the added parenthesis is helpful / recommended when doing something like the second example.
Also, == NetLinx gives a compiler error when it is used for assignment (for the obvious reasons.)
Muted = 1
Unmuted = 0
if (bMicMuted = Muted) etc
And yes, If(x) is a a perfectly valid statement - I use that kind of syntax all the time; any non-zero evaluation for x returns as true.