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

**URL:** <https://forum.arduino.cc/t/compilation-error-else-without-a-previous-if/1185814>\
**Category:** Programming\
**Created:** [November 5, 2023, 8:26pm UTC](https://forum.arduino.cc/t/compilation-error-else-without-a-previous-if/1185814 "2023-11-05T20:26:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![carhar](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/carhar/32/1013024_2.png) [@carhar](https://forum.arduino.cc/u/carhar)\
**Post date:** [November 5, 2023, 8:26pm UTC](https://forum.arduino.cc/t/compilation-error-else-without-a-previous-if/1185814/1 "2023-11-05T20:26:20Z")

</div>

I have written a very simple piece of code that lights a white LED when a button is pressed. I want the button state to be stored so the LED remains on until I press the button again.

I get the following error: Compilation error: 'else' without a previous 'if'

I can't work out where its gone wrong:

```arduino
int whiteledPin = 8;
int buttonPin = 4;
int lastState = digitalRead(4);

void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(whiteledPin, OUTPUT);

}

void loop() {
  int currState = digitalRead(buttonPin);
  if (currState == LOW); {
    digitalWrite(whiteledPin, HIGH);
  }
  else {
    digitalWrite(whiteledPin, LOW);
  }
  lastState = currState;
}

```

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [November 5, 2023, 8:34pm UTC](https://forum.arduino.cc/t/compilation-error-else-without-a-previous-if/1185814/2 "2023-11-05T20:34:55Z")

</div>

> [@carhar](#):
>
> `if (currState == LOW); {`

get rid of the semicolon

the syntax is

```arduino
if (condition) {
  •••
} else {
  •••
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 3, 2024, 8:35pm UTC](https://forum.arduino.cc/t/compilation-error-else-without-a-previous-if/1185814/3 "2024-05-03T20:35:50Z")

</div>

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