# Else' without a previous 'if'

**URL:** <https://forum.arduino.cc/t/else-without-a-previous-if/926319>\
**Category:** General Guidance\
**Created:** [November 17, 2021, 10:21pm UTC](https://forum.arduino.cc/t/else-without-a-previous-if/926319 "2021-11-17T22:21:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![onis024](https://avatars.discourse-cdn.com/v4/letter/o/46a35a/32.png) [@onis024](https://forum.arduino.cc/u/onis024)\
**Post date:** [November 17, 2021, 10:21pm UTC](https://forum.arduino.cc/t/else-without-a-previous-if/926319/1 "2021-11-17T22:21:45Z")

</div>

Cordial saludo soy nuevo conb la programacion de arduino, estoy realizando un proyecto de un carro detector de lineas, ceo los comandos y al compilar me da el siguiente error else' without a previous 'if'

este son los comando ingresados, no se donde me estoy equibocando

unsigned long x,y,z;  
float s1,s2,s3,s4,s5;  
int pwm=100;  
int color=350;  
int vl=pwm+60;  
int vh=pwm-60;  
void setup() {  
Serial.begin(9600);  
pinMode(3,OUTPUT);  
pinMode(4,OUTPUT);  
pinMode(5,OUTPUT);  
}

void loop(){  
ceros:  
analogWrite(3,0);  
analogWrite(5,0);  
inicio:  
digitalWrite(4,LOW);  
s1=analogRead(1);  
s2=analogRead(2);  
s3=analogRead(3);  
s4=analogRead(4);  
s5=analogRead(5);  
if (s3\>=color){  
analogWrite(3,pwm);  
analogWrite(5,pwm);  
x=millis();  
goto inicio;  
}  
else if (s1\>=color) //Extrema Izquierda  
{  
analogWrite(3,vl); //Motor Derecho  
analogWrite(5,vh); //Motor Izquierdo  
x=millis();  
goto inicio;  
}  
else if (s5\>=color) //Extrema Derecha  
{  
analogWrite(3,vh); //Motor Derecho  
analogWrite(5,vl); //Motor Izquierdo  
x=millis();  
goto inicio;  
}  
else if (s2\>=color); //Medio Izquierdo  
{  
analogWrite(3,pwm);  
analogWrite(5,vh);  
x=millis();  
goto inicio;  
}  
else if (s4\>=color); //Medeio Derecha  
{  
analogWrite(3,vh); //Motor Derecho  
analogWrite(5,pwm); //Motor Izquierdo  
x=millis();  
goto inicio;  
}  
else {  
digitalWrite(4,HIGH);  
y=millis();  
z=y=x;  
if(z\<=1000)  
{  
goto inicio;  
}  
else {  
goto ceros;  
}  
}  
}

---

<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:** [November 17, 2021, 10:26pm UTC](https://forum.arduino.cc/t/else-without-a-previous-if/926319/2 "2021-11-17T22:26:58Z")

</div>

[Read the forum guidelines](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966) to see how to properly post code and some hints on how to get the most from this forum.  
Use the IDE autoformat tool (ctrl-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.

---

<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:** [November 17, 2021, 10:33pm UTC](https://forum.arduino.cc/t/else-without-a-previous-if/926319/3 "2021-11-17T22:33:01Z")

</div>

```cpp
else if (s2 >= color);
else if (s4 >= color);

```

Ending the else if (or any if) with a semi colon is a mistake.

Here is your code posted properly in code tags and formatted. I removed the bad semicolons and now the code compiles with no warnings or errors.

```cpp
unsigned long x, y, z;
float s1, s2, s3, s4, s5;
int pwm = 100;
int color = 350;
int vl = pwm + 60;
int vh = pwm - 60;

void setup()
{
   Serial.begin(9600);
   pinMode(3, OUTPUT);
   pinMode(4, OUTPUT);
   pinMode(5, OUTPUT);
}

void loop()
{
ceros:
   analogWrite(3, 0);
   analogWrite(5, 0);
inicio:
   digitalWrite(4, LOW);
   s1 = analogRead(1);
   s2 = analogRead(2);
   s3 = analogRead(3);
   s4 = analogRead(4);
   s5 = analogRead(5);
   if (s3 >= color)
   {
      analogWrite(3, pwm);
      analogWrite(5, pwm);
      x = millis();
      goto inicio;
   }
   else if (s1 >= color) //Extrema Izquierda
   {
      analogWrite(3, vl); //Motor Derecho
      analogWrite(5, vh); //Motor Izquierdo
      x = millis();
      goto inicio;
   }
   else if (s5 >= color) //Extrema Derecha
   {
      analogWrite(3, vh); //Motor Derecho
      analogWrite(5, vl); //Motor Izquierdo
      x = millis();
      goto inicio;
   }
   else if (s2 >= color) //Medio Izquierdo ******* removed ;
   {
      analogWrite(3, pwm);
      analogWrite(5, vh);
      x = millis();
      goto inicio;
   }
   else if (s4 >= color) //Medeio Derecha ******* removed ;
   {
      analogWrite(3, vh); //Motor Derecho
      analogWrite(5, pwm); //Motor Izquierdo
      x = millis();
      goto inicio;
   }
   else
   {
      digitalWrite(4, HIGH);
      y = millis();
      z = y = x;
      if (z <= 1000)
      {
         goto inicio;
      }
      else
      {
         goto ceros;
      }
   }
}

```

Since the 1980s we don't use goto. In C++ ( the language of Arduino) we use [functions](https://www.tutorialspoint.com/arduino/arduino_functions.htm).

---

<div class="post-metadata">

**Author:** ![onis024](https://avatars.discourse-cdn.com/v4/letter/o/46a35a/32.png) [@onis024](https://forum.arduino.cc/u/onis024)\
**Post date:** [November 17, 2021, 10:57pm UTC](https://forum.arduino.cc/t/else-without-a-previous-if/926319/4 "2021-11-17T22:57:36Z")

</div>

> [@groundFungus](#):
>
> Ending the else if (or any if) with a semi colon is a mistake.
> 
> Here is your code posted properly in code tags and formatted. I removed the bad semicolons and now the code compiles with no warnings or errors.

Gracias ya me funciono

---

<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 16, 2022, 10:58pm UTC](https://forum.arduino.cc/t/else-without-a-previous-if/926319/5 "2022-05-16T22:58:27Z")

</div>

Closed on May 16, 2022, 10:58pm UTC

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