I'm going to add a little delay and we'll see how your two examples act differently:
void loop() {
if(x <= 5){
digitalWrite(ledPin, LOW);
x++;
} else {
digitalWrite(ledPin, HIGH);
}
delay(1000);
}
In this first example the LED is turned off while X is 0, 1, 2, 3, 4, and 5 (about 6 seconds) and turns ON at the 7th iteration.
void loop() {
if(x <= 5){
digitalWrite(ledPin, LOW);
x++;
}
if(x > 5) {
digitalWrite(ledPin, HIGH);
}
delay(1000);
}
In this second example the LED is turned off while X is 0, 1, 2, 3, 4 (5 seconds) but in the 6th iteration when X==5 the value of X is incremented to 6 which triggers the second 'if' and turns the LED on. This causes the LED to come on about a second earlier.
You could fix that particular problem by swapping the two statements:
void loop() {
if(x > 5) {
digitalWrite(ledPin, HIGH);
}
if(x <= 5){
digitalWrite(ledPin, LOW);
x++;
}
delay(1000);
}
But then if you wanted to repeat the sequence (off 6 seconds, on 1 second) by setting x back to 0, the simple change would have a problem similar to the original:
void loop() {
if(x > 5) {
digitalWrite(ledPin, HIGH);
x = 0;
}
if(x <= 5){
digitalWrite(ledPin, LOW);
x++;
}
delay(1000);
}
The LED would be on for only a few microseconds until the second 'if' triggered and turned it off.
You can't just swap the statements this time because that would bring back the original problem and you'd have 5 seconds off and 1 second on rather than 6 and 1.
The way to fix it is to use the 'else':
void loop() {
if(x <= 5){
digitalWrite(ledPin, LOW);
x++;
} else {
digitalWrite(ledPin, HIGH);
x = 0;
}
delay(1000);
}