Compilation error: 'else' without a previous 'if'

Hey guys i have followed along with spaceship interface and come up with a complation error else without a previous if.
i have both if and else but have something not right
any help would be greatly appreciated
my code is below

int switchState = 0;
void setup() {
  pinMode (3, OUTPUT);
  pinMode (4, OUTPUT);
  pinMode (5, OUTPUT);
  pinMode (2, INPUT);
}
void loop() {
  switchState = digitalRead (2);
// reading for power on pin 2 connected to button
if( switchState == LOW); {
  // the button is not pressed
digitalWrite (3, HIGH);
digitalWrite (4, LOW);
digitalWrite (5, LOW);
}
else { //the button is pressed
digitalWrite (3, LOW);
digitalWrite (4, LOW);
digitalWrite (5, HIGH);
delay (250); //wait quater second 
//toggle leds
digitalWrite (4, HIGH);
digitalWrite (5, LOW);
delay(250); // wait quarter second
}
}//go back to begining of loop 

The error is on this line. It's a very common beginner error. Do some searches and figure out what it is.

1 Like

If you do an AUTO-FORMAT, (CTRL-T in the IDE) it will become more obvious.

1 Like

ok thank you for the hints it was ; this guy
i believe it makes it a statment
and " if " is not a statement
and crtl t did make it mor obvious
thank you paul and lastchance

The ; ends a statement. So, later, when the compiler finds the else, it looks for an if to match it with. But it can't match it with the if you wanted it to match with, because that if is ended.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.