Home AMX User Forum NetLinx Studio

Difference Between = and ==

TurnipTruckTurnipTruck Junior MemberPosts: 1,485
Greetings,

I am examining some code. What is the difference between = and ==

Thanks.

Comments

  • ekeppelekeppel Junior Member Posts: 37
    It is a relational operator that can be substituted with a single = in most cases. You can look up relational operators in NS help to see some info on it.

    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
  • Chip MoodyChip Moody Junior Member Posts: 727
    = can serve two functions, assignment and comparison - I.E.:

    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. :)
  • usefletchusefletch Junior Member Posts: 3
    Chip is correct. Netlinx compiler allows both terms. The only place it seems required is in ambiguous code. Where the compiler can not differentiate assignment or comparison.
    As a C/C++ programmer, the netlinx compiler allows my normal usage of both to compile just fine.
  • Joe HebertJoe Hebert Junior Member Posts: 2,159
    I agree with all assessments, however, I believe the correct answer to the original question is...one equal sign. Ok, stupid joke. :)

    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.
  • jjamesjjames Just another dude Posts: 2,905
    Joe Hebert wrote:
    I agree with all assessments, however, I believe the correct answer to the original question is...one equal sign. Ok, stupid joke. :)

    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.
    I use that all the time. And I will also do:
    IF(!x)
    {
      // Do stuff if it is zero
    }
    

    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....
    IF(x==FALSE && y==TRUE && z==FALSE)
    
    Rather than saying:
    IF((!x) && (y) && (!z))
    
    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.)
  • NMarkRobertsNMarkRoberts Junior Member Posts: 455
    I take this very literal coding rule a step further when dealing with muting, which can be confusing as it is reversed sense (muting on means sound off)

    Muted = 1
    Unmuted = 0

    if (bMicMuted = Muted) etc
  • DHawthorneDHawthorne Old Timer Posts: 4,584
    I always use == for comparisons, even though a single = may work. It's partly from my C++ background, where using an = for comparisons assigns at the same time, but mostly it's so there can be no possible doubt in case I missed a situation the compiler finds ambiguous.

    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.
Sign In or Register to comment.