Understanding a Loop.

Hi,

Can some kind person explain how this loop would repeat indefinitely, please?

void loop()
{
  for (int x = 0; x < 3; x++)
  {
    if (digitalRead(buttonPins[x]) == LOW)
    {
      Serial.print("The button on pin ");
      Serial.print(buttonPins[x]);
      Serial.println(" is pressed");
    }
  }
}

What I am unsure about is how the loop works once x=3.

Does x revert back to 0 once x=3 so the loop keeps looping or does it only loop 3 times then become false and the loop stops?

Would it be better to use unsigned int?

Thanks,

Martin

The for loop exits when X == 3, so the loop () function exits and gets called again, so the for loop executes again from zero.

Hi AWOL,

Thanks very much for the reply, that makes sense now.

Would it be better to use unsigned int due to a possible overflow problem with using int?

Thanks,

Martin

Even a byte (unsigned char) doesn't overflow for a count of three.

Hi AWOL,

Thanks, they didn't really explain that in the guide. :o

Martin