# Code not working (else with no previous if)

**URL:** <https://forum.arduino.cc/t/code-not-working-else-with-no-previous-if/850846>\
**Category:** Programming\
**Created:** [April 19, 2021, 4:23pm UTC](https://forum.arduino.cc/t/code-not-working-else-with-no-previous-if/850846 "2021-04-19T16:23:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![anma1133](https://avatars.discourse-cdn.com/v4/letter/a/b4bc9f/32.png) [@anma1133](https://forum.arduino.cc/u/anma1133)\
**Post date:** [April 19, 2021, 4:23pm UTC](https://forum.arduino.cc/t/code-not-working-else-with-no-previous-if/850846/1 "2021-04-19T16:23:13Z")

</div>

#include \<Servo.h\>

Servo penguin; // create servo object to control a servo  
// twelve servo objects can be created on most boards  
Servo turn;

// constants won't change. They're used here to set pin numbers:  
const int buttonPin = 2; // the number of the pushbutton pin  
const int ledPin = 13; // the number of the LED pin  
const int buttongreen = 8; // the number of the pushbutton pin  
const int buttonred = 4; // the number of the pushbutton pin

// variables will change:  
int buttonState = 0; // variable for reading the pushbutton status  
int buttonStategreen = 0;  
int buttonStatered = 0;  
int pos = 0; // variable to store the servo position  
int posg = 0;  
int posr = 0;

void setup() {  
// initialize the LED pin as an output:  
pinMode(ledPin, OUTPUT);  
// initialize the pushbutton pin as an input:  
pinMode(buttonPin, INPUT);  
pinMode(buttongreen, INPUT);  
pinMode(buttonred, INPUT);  
penguin.attach(9); // attaches the servo on pin 9 to the servo object  
turn.attach(7); // attaches the servo on pin 9 to the servo object  
}

void loop() {  
// read the state of the pushbutton value:  
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:  
if (buttonState == HIGH) {  
// turn LED on:  
digitalWrite(ledPin, HIGH);  
{  
for (pos = 180; pos \>= 90; pos -= 5) { // goes from 0 degrees to 180 degrees  
// in steps of 1 degree  
penguin.write(pos); // tell servo to go to position in variable 'pos'  
delay(1); // waits 15ms for the servo to reach the position  
}  
for (pos = 90; pos \<= 180; pos += 1) { // goes from 180 degrees to 0 degrees  
penguin.write(pos); // tell servo to go to position in variable 'pos'  
delay(100); // waits 15ms for the servo to reach the position  
}  
// read the state of the pushbutton value:  
buttonStategreen = digitalRead(buttongreen);  
}  
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:  
;} else if (buttonStategreen == HIGH); {  
// turn LED on:  
digitalWrite(ledPin, HIGH);  
{  
for (posg = 180; posg \>= 90; posg -= 1) { // goes from 0 degrees to 180 degrees  
// in steps of 1 degree  
turn.write(posg); // tell servo to go to position in variable 'pos'  
delay(1); // waits 15ms for the servo to reach the position  
}  
for (posg = 90; posg \<= 180; posg += 1) { // goes from 180 degrees to 0 degrees  
turn.write(posg); // tell servo to go to position in variable 'pos'  
delay(5); // waits 15ms for the servo to reach the position  
}  
} }  
buttonStatered = digitalRead(buttonred);  
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:  
; else if (buttonStatered == HIGH); {  
// turn LED on:  
digitalWrite(ledPin, HIGH);  
{  
for (posr = 180; posr \>= 90; posr -= 1) { // goes from 0 degrees to 180 degrees  
// in steps of 1 degree  
turn.write(posr); // tell servo to go to position in variable 'pos'  
delay(1); // waits 15ms for the servo to reach the position  
}  
for (posg = 90; posr \<= 180; posr += 1) { // goes from 180 degrees to 0 degrees  
turn.write(posr); // tell servo to go to position in variable 'pos'  
delay(5); // waits 15ms for the servo to reach the position  
}  
} }  
else {  
// turn LED off:  
digitalWrite(ledPin, LOW);  
}  
}

I can't seem to figure out why I am getting the error message else with no previous if

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [April 19, 2021, 4:26pm UTC](https://forum.arduino.cc/t/code-not-working-else-with-no-previous-if/850846/2 "2021-04-19T16:26:48Z")

</div>

Read the [how get the most out of this forum sticky](https://forum.arduino.cc/index.php?topic=712199.0) to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code in code tags.

Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

```cpp
; else if (buttonStatered == HIGH); {

```

Do else if statements start and end with a semi colon? I don't think so.

```cpp
            delay(5); // waits 15ms for the servo to reach the position
         }
      }
   }
   buttonStatered = digitalRead(buttonred); // ******* this interrupts the if-else if-else chain
   // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
   ; else if (buttonStatered == HIGH); // ****** extra semi colons.
   {

```

You could move the digitalRead into the if else and eliminate the offending line.  
`else if (digitalRead(buttonred) == HIGH)`

---

<div class="post-metadata">

**Author:** ![anma1133](https://avatars.discourse-cdn.com/v4/letter/a/b4bc9f/32.png) [@anma1133](https://forum.arduino.cc/u/anma1133)\
**Post date:** [April 19, 2021, 4:53pm UTC](https://forum.arduino.cc/t/code-not-working-else-with-no-previous-if/850846/3 "2021-04-19T16:53:06Z")

</div>

I'm a beginner and not familiar with the code yet so you may have to bear with me. I moved the digital read like you suggested, but I still get the error message. The exact error message is

Arduino: 1.8.5 (Windows 7), Board: "Arduino/Genuino Uno"

Build options changed, rebuilding all  
H:\Documents\Arduino\slingshotver3\slingshotver3.ino: In function 'void loop()':

slingshotver3:91: error: 'else' without a previous 'if'

```
else if (digitalRead(buttonred)== HIGH); {

^

```

slingshotver3:105: error: 'else' without a previous 'if'

else {

^

exit status 1  
'else' without a previous 'if'

Is there anything else I can do?

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [April 19, 2021, 5:33pm UTC](https://forum.arduino.cc/t/code-not-working-else-with-no-previous-if/850846/4 "2021-04-19T17:33:37Z")

</div>

When you make changes to your posted code, please post the new version so that we can keep up. Make sure to post with code tags as the forum guidelines instruct.

```cpp
else if (digitalRead(buttonred)== HIGH);

```

Statements like if, else if and else DO NOT end with semi colons.

---

<div class="post-metadata">

**Author:** ![Ballscrewbob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ballscrewbob/32/950458_2.png) [@Ballscrewbob](https://forum.arduino.cc/u/Ballscrewbob)\
**Post date:** [April 19, 2021, 9:20pm UTC](https://forum.arduino.cc/t/code-not-working-else-with-no-previous-if/850846/5 "2021-04-19T21:20:44Z")

</div>

Unlisted on April 19, 2021, 9:20pm UTC



---

<div class="post-metadata">

**Author:** ![pert](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/pert/32/217111_2.png) [@pert](https://forum.arduino.cc/u/pert)\
**Post date:** [April 19, 2021, 9:44pm UTC](https://forum.arduino.cc/t/code-not-working-else-with-no-previous-if/850846/6 "2021-04-19T21:44:04Z")

</div>

Listed on April 19, 2021, 9:44pm UTC



---

<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:** [August 17, 2021, 9:44pm UTC](https://forum.arduino.cc/t/code-not-working-else-with-no-previous-if/850846/7 "2021-08-17T21:44:30Z")

</div>

Closed on August 17, 2021, 9:44pm UTC

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