Home AMX User Forum NetLinx Studio

IF OR Statement

Can someone tell me what i have wrong with the syntax of this command

IF ((x <> 'ON')or(x <> 'OFF')){do this}

Basiclly i want to do something if x is not equal to on or off.

Thanks

Comments

  • ericmedleyericmedley Posts: 4,177
    xrmichael wrote: »
    Can someone tell me what i have wrong with the syntax of this command

    IF ((x <> 'ON')or(x <> 'OFF')){do this}

    Basiclly i want to do something if x is not equal to on or off.

    Thanks


    it should be:
    IF( X<>'ON' OR X<>'OFF')
      {
      // DO SOMETHING
      }
    
    
  • mpullinmpullin Posts: 949
    ericmedley wrote: »
    it should be:
    IF( X<>'ON' OR X<>'OFF')
      {
      // DO SOMETHING
      }
    
    This expression will always evaluate to true!

    You want this:
    if(X <> 'ON' && X <> 'OFF') REBOOT(0);
    
    or this
    if( !(X == 'ON' || X == 'OFF')) REBOOT(0);
    
  • ericmedleyericmedley Posts: 4,177
    mpullin wrote: »
    This expression will always evaluate to true!

    You want this:
    if(X <> 'ON' && X <> 'OFF') REBOOT(0);
    
    or this
    if( !(X == 'ON' || X == 'OFF')) REBOOT(0);
    

    I wasn't passing judgement on the logic. I was just showing the proper syntax. :D
  • Cheers

    Thanks, back on track again now.
  • mpullinmpullin Posts: 949
    ericmedley wrote: »
    I wasn't passing judgement on the logic. I was just showing the proper syntax. :D
    I didn't see anything wrong with his syntax :(

    Edit: Does OR have to be capitalized? I always use || for logical or and && for logical and.
  • Joe HebertJoe Hebert Posts: 2,159
    mpullin wrote: »
    Edit: Does OR have to be capitalized?
    No, Netlinx is case insensitive
  • Joe HebertJoe Hebert Posts: 2,159
    mpullin wrote: »
    I didn't see anything wrong with his syntax :(
    Correct, there wasn?t anything wrong with the original syntax (assuming that x is a char array and not an integer) and the syntax that Eric posted is exactly the same as the original post sans some parens that were doing no harm. As you pointed out, the logic was the problem.
  • DanielDaniel Posts: 9
    It's not OR you need, it's AND...

    if (X is not 'ON') and (is not 'OFF' )

    or if( X <> 'ON' and X <> 'OFF') { // do something }

    with or, it's always true because X will always be "not 'ON" or "not OFF". One of those 2 will always be true so the condition will always fire.
Sign In or Register to comment.