[SOLVED] IF/ELSE statement without Comparison Operators

I found the following example of code (link) to make a momentary pushbutton act as a toggle switch. With the exception of one extra curly bracket at the very end of the sketch (which I deleted), it compiles and works correctly on my Arduino Uno.

//Let's say you have your push button on pin 2
int switchState = 0; // actual read value from pin2
int oldSwitchState = 0; // last read value from pin2

int lightsOn = 0; // is the switch on = 1 or off = 0

void setup() {                
  pinMode(2, INPUT); // push button     
  pinMode(3, OUTPUT); // anything you want to control using a switch e.g. a Led
}

void loop() {
  switchState = digitalRead(2); // read the pushButton State
  if (switchState != oldSwitchState) // catch change
  {
    oldSwitchState = switchState;
    if (switchState == HIGH)
    {
      // toggle
      lightsOn = !lightsOn;
    }
  }

  if(lightsOn)
  {
     digitalWrite(3, HIGH);   // set the LED on
   } else {
      digitalWrite(3, LOW);   // set the LED off
    }
  }
}

My question is about the last IF/ELSE statement at the end of the code:

if(lightsOn)
  {
     digitalWrite(3, HIGH);   // set the LED on
   } else {
      digitalWrite(3, LOW);   // set the LED off
    }

The Arduino Reference page for the IF statement and the IF/ELSE statement both say that Comparison Operators are required for the IF portion of the statement. The above code not have a Comparison Operator but it does work correctly. Why? Is the code...

if(lightsOn)

... a shortcut that means: if (lightsOn)==HIGH?

Matt

The equality, and other comparison, operators return true or false.

So, if(a > b) will become either if(true) or if(false). Since true is any non-zero value and false is 0, there doesn't need to be a comparison operator. In the example, the statement is equivalent to
if(lightsOn == HIGH) (or any other non-zero value).

Thanks for the quick reply Paul.

I'm pretty sure I understand now. Would a more accurate description be: if(lightsOn != LOW)

Do you know of any tutorials/reference/further reading that talks about examples like this? To me this sounds like something learned from experience since it isn't mentioned in the Arduino Reference guides. Thanks again.

Matt

mmc_ardcc:
I'm pretty sure I understand now. Would a more accurate description be: if(lightsOn != LOW)

think like this:

if(whatever is contained in here evaluates to non-zero)
{
  doThis();
  andThis()
  //etc...
}

or

if(whatever is contained in here evaluates to non-zero)
doJustThis();
andContinueOn();

Thanks Delta_G & BulldogLowell! I'm marking this thread as [SOLVED]

Matt

Would a more accurate description be: if(lightsOn != LOW)

That's another way to look at it. Given that lightsOn will hold 0 or 1, they mean the same thing.

To me this sounds like something learned from experience

Yes, but that doesn't mean that it is good practice. In fact, it isn't. There is nothing to be gained from skipping typing a few keystrokes. The compiler will generate the same code, but people reading it will have a harder time deciphering what is meant.