# Error: Expected unqualified-id before 'else'

**URL:** <https://forum.arduino.cc/t/error-expected-unqualified-id-before-else/353573>\
**Category:** Programming\
**Created:** [December 19, 2015, 9:06pm UTC](https://forum.arduino.cc/t/error-expected-unqualified-id-before-else/353573 "2015-12-19T21:06:43Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![JVilkas](https://avatars.discourse-cdn.com/v4/letter/j/6de8d8/32.png) [@JVilkas](https://forum.arduino.cc/u/JVilkas)\
**Post date:** [December 19, 2015, 9:06pm UTC](https://forum.arduino.cc/t/error-expected-unqualified-id-before-else/353573/1 "2015-12-19T21:06:43Z")

</div>

Hi,

Im a beginner and just started learning how to use my Arduino UNO.  
Here is my first code:

int switchState = 0;

void setup() {  
pinMode(3,OUTPUT);  
pinMode(4,OUTPUT);  
pinMode(5,OUTPUT);  
pinMode(2,INPUT);  
}

void loop() {  
switchState = digitalRead(2)  
// digitalRead checks for voltage

;if(switchState == LOW);  
// two equal signs compare two things  
digitalWrite(3, LOW); //green LED  
digitalWrite(4, LOW); //red LED  
digitalWrite(5, HIGH);// red LED  
}

else {  
digitalWrite(3,LOW);  
digitalWrite(4,LOW) ;  
digitalWrite(5,HIGH);

delay(250);  
digitalWrite(4,HIGH);  
digitalWrite(5,LOW);  
delay(250);

}

* * *

Im not sure how to fix it. When I try to upload it to the UNO, it says this:  
Error: Expected unqualified-id before 'else'

Can someone please help me?

---

<div class="post-metadata">

**Author:** ![LROBBINS](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/lrobbins/32/1126393_2.png) [@LROBBINS](https://forum.arduino.cc/u/LROBBINS)\
**Post date:** [December 19, 2015, 11:05pm UTC](https://forum.arduino.cc/t/error-expected-unqualified-id-before-else/353573/2 "2015-12-19T23:05:39Z")

</div>

This line looks very strange to me:

```cpp
;if(switchState == LOW);

```

---

<div class="post-metadata">

**Author:** ![Martin-X](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/martin-x/32/1126438_2.png) [@Martin-X](https://forum.arduino.cc/u/Martin-X)\
**Post date:** [December 19, 2015, 11:09pm UTC](https://forum.arduino.cc/t/error-expected-unqualified-id-before-else/353573/3 "2015-12-19T23:09:33Z")

</div>

You have a few problems with semicolons and curly braces.

Have a look at these:

[How to use this forum - please read.](http://forum.arduino.cc/index.php?topic=148850.0)

> **[; - Arduino Reference](https://www.arduino.cc/reference/en/language/structure/further-syntax/semicolon/)**
>
> The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.

> **[{} - Arduino Reference](https://www.arduino.cc/reference/en/language/structure/further-syntax/curlybraces/)**
>
> The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.

---

<div class="post-metadata">

**Author:** ![JVilkas](https://avatars.discourse-cdn.com/v4/letter/j/6de8d8/32.png) [@JVilkas](https://forum.arduino.cc/u/JVilkas)\
**Post date:** [December 20, 2015, 2:03am UTC](https://forum.arduino.cc/t/error-expected-unqualified-id-before-else/353573/4 "2015-12-20T02:03:47Z")

</div>

Thanks for your responses!

---

<div class="post-metadata">

**Author:** ![bob21](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@bob21](https://forum.arduino.cc/u/bob21)\
**Post date:** [December 26, 2015, 7:13pm UTC](https://forum.arduino.cc/t/error-expected-unqualified-id-before-else/353573/5 "2015-12-26T19:13:30Z")

</div>

I'm in exactly the same situation, working through the same project and am having the same problems!

I've tried everything suggested in this thread but nothing has helped, my code looks just like JVilkas.

Did you manage to sort it?

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [December 26, 2015, 7:26pm UTC](https://forum.arduino.cc/t/error-expected-unqualified-id-before-else/353573/6 "2015-12-26T19:26:09Z")

</div>

> bob21:  
> I've tried everything suggested in this thread but nothing has helped, my code **looks just like** JVilkas.

I suspect there is a difference - perhaps a very small one.

Post your code!  
And please use the code button \</\> `so your code looks like this` and is easy to copy to a text editor

...R

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [December 26, 2015, 7:29pm UTC](https://forum.arduino.cc/t/error-expected-unqualified-id-before-else/353573/7 "2015-12-26T19:29:09Z")

</div>

The problems were pointed out in the previous replies.  
if statements do do end with a semicolon  
Code lines do not begin with a semicolon

Did you fix all of them ?

Please post the fixed code here to get more help.

Where did you get the code ?

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [December 26, 2015, 7:30pm UTC](https://forum.arduino.cc/t/error-expected-unqualified-id-before-else/353573/8 "2015-12-26T19:30:49Z")

</div>

switchState = digitalRead(2)  
Change to:  
switchState = digitalRead(2);  
And:  
;if(switchState == LOW);  
Get rid of both **;**

---

<div class="post-metadata">

**Author:** ![bob21](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@bob21](https://forum.arduino.cc/u/bob21)\
**Post date:** [December 26, 2015, 7:33pm UTC](https://forum.arduino.cc/t/error-expected-unqualified-id-before-else/353573/9 "2015-12-26T19:33:57Z")

</div>

hey - sorry  
here's my code!

\<int switchState = 0;

void setup() {  
pinMode(3, OUTPUT) ;  
pinMode(4, OUTPUT) ;  
pinMode(5, OUTPUT) ;  
pinMode(2, INPUT) ;  
}

void loop() {  
switchState = digitalRead(2);  
if (switchState == LOW)

digitalWrite(3, HIGH);  
digitalWrite(4, LOW);  
digitalWrite(5, LOW);

}

else {  
digitalWrite(3, LOW);  
digitalWrite(4, HIGH);  
digitalWrite(5, HIGH);  
}

> 

the code is from the book that came with the arduino!

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [December 26, 2015, 7:35pm UTC](https://forum.arduino.cc/t/error-expected-unqualified-id-before-else/353573/10 "2015-12-26T19:35:51Z")

</div>

if (switchState == LOW)  
Change to:  
if (switchState == LOW) {

---

<div class="post-metadata">

**Author:** ![bob21](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@bob21](https://forum.arduino.cc/u/bob21)\
**Post date:** [December 26, 2015, 7:42pm UTC](https://forum.arduino.cc/t/error-expected-unqualified-id-before-else/353573/11 "2015-12-26T19:42:46Z")

</div>

legend, sorted!! Thanks!!!

---

<div class="post-metadata">

**Author:** ![charliew4](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/charliew4/32/1185778_2.png) [@charliew4](https://forum.arduino.cc/u/charliew4)\
**Post date:** [December 2, 2017, 9:39pm UTC](https://forum.arduino.cc/t/error-expected-unqualified-id-before-else/353573/12 "2017-12-02T21:39:14Z")

</div>

i have the same problem' this is my code [in quotations] please help me

"// Keypad - Version: Latest  
#include \<Key.h\>  
#include \<Keypad.h\>  
#include \<Keypad.h\>

char\* password = "159"; // change the password here, just pick any 3 numbers  
int position = 0;  
const byte ROWS = 4;  
const byte COLS = 4;  
char keys[ROWS][COLS] = {  
{'1','2','3','A'},  
{'4','5','6','B'},  
{'7','8','9','C'},  
{'\*','0','#','D'}  
};

byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad  
byte colPins[COLS] = {9, 8, 7, 6};  
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );  
int RedpinLock = 12;  
int GreenpinUnlock = 13;

void setup()  
{  
pinMode(RedpinLock, OUTPUT);  
pinMode(GreenpinUnlock, OUTPUT);  
}

void loop()  
{  
char key = keypad.getKey();  
if (key == '\*' || key == '#')

if (key == password[position])  
digitalWrite(RedpinLock, HIGH);  
digitalWrite(GreenpinUnlock, LOW);  
}  
else  
{  
digitalWrite(RedpinLock, LOW);  
digitalWrite(GreenpinUnlock, HIGH);  
}  
}  
"

---

<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:** [December 2, 2017, 9:41pm UTC](https://forum.arduino.cc/t/error-expected-unqualified-id-before-else/353573/13 "2017-12-02T21:41:30Z")

</div>

`if (key == password[position])`see reply #9

And please remember to use code . . .oh god, what's the point?

---

<div class="post-metadata">

**Author:** ![DrAzzy](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@DrAzzy](https://forum.arduino.cc/u/DrAzzy)\
**Post date:** [December 2, 2017, 9:56pm UTC](https://forum.arduino.cc/t/error-expected-unqualified-id-before-else/353573/14 "2017-12-02T21:56:48Z")

</div>

That message means you used an else statement that was not immediately preceded by an if statement, usually due to messed up/missing curly braces. In both of your cases, you forgot an opening brace after the if

---

<div class="post-metadata">

**Author:** ![javierj91](https://avatars.discourse-cdn.com/v4/letter/j/ce73a5/32.png) [@javierj91](https://forum.arduino.cc/u/javierj91)\
**Post date:** [February 8, 2018, 5:02am UTC](https://forum.arduino.cc/t/error-expected-unqualified-id-before-else/353573/15 "2018-02-08T05:02:15Z")

</div>

SO I AM COPYING THIS CODE FROM THE BOOK A I KEEP GETTING ERROR MESSAGE CAN SOMEONE PLEASE HELP ME DEBUG? THANK YOU

int switchState = 0;  
void setup () {  
pinMode(3, OUTPUT);  
pinMode(4, OUTPUT);  
pinMode(5, OUTPUT);  
pinMode(2, INPUT);  
}  
void loop(){  
switch = digitalRead(2);  
if (switchState == LOW)  
// the button is not pressed  
digitalWrite(3, HIGH); // green LED  
digitalWrite(4, LOW); // red LED  
digitalWrite(5, LOW); // red LED  
}  
else { // the button is pressed  
digitalWrite(3, LOW);  
digitalWrite(4, LOW);  
digitalWrite(5, HIGH);  
delay(250) // wait or the quarter second  
// toggle the LEDs  
digitalWrite(4, HIGH);  
digitalWrite(5, LOW);  
delay(250); // wait for the quarter second  
}  
} // go back to the beginning of loop

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [February 8, 2018, 5:21am UTC](https://forum.arduino.cc/t/error-expected-unqualified-id-before-else/353573/16 "2018-02-08T05:21:14Z")

</div>

@javierj91

**Please do not hijack other peoples thread, start your own.**

Don't yell at us.

Always use code tags (click the \</\> button in the posting menu) when attaching your sketch.

Every { needs a friend }

'switch' is not defined as a variable.

Each code line needs a ; at the end of it.

---

<div class="post-metadata">

**Author:** ![DrAzzy](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@DrAzzy](https://forum.arduino.cc/u/DrAzzy)\
**Post date:** [February 8, 2018, 6:33am UTC](https://forum.arduino.cc/t/error-expected-unqualified-id-before-else/353573/17 "2018-02-08T06:33:16Z")

</div>

Wait, so is there a book lots of people are getting with a missing openbrace in an example?

@larryd- I wouldn't call that hijacking, seeing as he seems to be working with near identical code (seems like many people in this thread are looking at same book)

---

<div class="post-metadata">

**Author:** ![Alusine](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@Alusine](https://forum.arduino.cc/u/Alusine)\
**Post date:** [July 29, 2018, 3:44pm UTC](https://forum.arduino.cc/t/error-expected-unqualified-id-before-else/353573/18 "2018-07-29T15:44:07Z")

</div>

this was what worked for me, you need to add another '}' at the end of your program but i sill have problems uploading to my arduino

int switchState = 0;  
void setup() {  
pinMode(3,OUTPUT);  
pinMode(4,OUTPUT);  
pinMode(5,OUTPUT);  
pinMode(2,INPUT);  
}

void loop() {  
switchState = digitalRead(2);  
// this is a comment  
if (switchState == LOW){  
// the button is not pressed

digitalWrite(3, HIGH); //green LED  
digitalWrite(4, LOW); //red LED  
digitalWrite(5, LOW); //red LED  
}

else { // the button is pressed  
digitalWrite(3, LOW);  
digitalWrite(4, LOW);  
digitalWrite(5, HIGH);

delay(250); //wait for a quarter of a second  
//toggle the LED  
digitalWrite(4, HIGH);  
digitalWrite(5, LOW);  
delay(250); //wait for a quarter second  
}}

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [July 29, 2018, 5:37pm UTC](https://forum.arduino.cc/t/error-expected-unqualified-id-before-else/353573/19 "2018-07-29T17:37:43Z")

</div>

> Alusine:  
> this was what worked for me, you need to add another '}' at the end of your program **but i sill have problems uploading to my arduino**  
> ...  
> ...

There is a different section for that; either post in [Installation & Troubleshooting](http://forum.arduino.cc/index.php?board=2.0) or [Avrdude, stk500, Bootloader issues](http://forum.arduino.cc/index.php?board=119.0).

---

<div class="post-metadata">

**Author:** ![aseil17](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@aseil17](https://forum.arduino.cc/u/aseil17)\
**Post date:** [February 25, 2019, 3:18am UTC](https://forum.arduino.cc/t/error-expected-unqualified-id-before-else/353573/20 "2019-02-25T03:18:58Z")

</div>

/\*

- made by technvation
- uses open source blynk app to send data over the interent
- This example code is in public domain.
- feel free to modify, change, improve  
\*/  
#define BLYNK\_PRINT Serial  
#include \<Ultrasonic.h\>

#include \<SPI.h\>  
#include \<WiFi101.h\>  
#include \<BlynkSimpleMKR1000.h\>

// You should get Auth Token in the Blynk App.  
// Go to the Project Settings (nut icon).  
char auth[] = "12f7a8cb125e4e688bc22cedacd0ced9";

// Your WiFi credentials.  
// Set password to "" for open networks.  
char ssid[] = "ADIT's iPhone";  
char pass[] = "A93rtur3";

BlynkTimer timer;  
int distance = 0;  
int thresh[3] = {25, 50, 75};  
Ultrasonic ultrasonic(12, 13);

WidgetLED green(V1);  
WidgetLED orange(V2);  
WidgetLED red(V3);

void setup()  
{  
// Debug console  
Serial.begin(9600);  
Blynk.begin(auth, ssid, pass);  
[//Blynk.begin](https://Blynk.begin)(auth, ssid, pass, "[blynk-cloud.com](http://blynk-cloud.com)", 8442);  
[//Blynk.begin](https://Blynk.begin)(auth, ssid, pass, IPAddress(192,168,1,100), 8442);  
}

void loop()  
{  
distance = ultrasonic.distanceRead();  
Serial.println(distance);  
Blynk.run();

if(distance\<thresh[0]){  
green.on();  
}  
else if(distance\<thresh[1])  
green.on();  
orange.on();  
}  
else if(distance\<thresh[2])  
green.on();  
orange.on();  
red.on();  
}  
else{  
green.off();  
orange.off();  
red.off();  
delay(100);  
}

[Next page](https://forum.arduino.cc/t/error-expected-unqualified-id-before-else/353573.md?page=2)
