# Code gives me error (else without a previos if) when uploading the code

**URL:** <https://forum.arduino.cc/t/code-gives-me-error-else-without-a-previos-if-when-uploading-the-code/1272447>\
**Category:** Programming\
**Created:** [June 16, 2024, 8:04pm UTC](https://forum.arduino.cc/t/code-gives-me-error-else-without-a-previos-if-when-uploading-the-code/1272447 "2024-06-16T20:04:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrt190](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@mrt190](https://forum.arduino.cc/u/mrt190)\
**Post date:** [June 16, 2024, 8:04pm UTC](https://forum.arduino.cc/t/code-gives-me-error-else-without-a-previos-if-when-uploading-the-code/1272447/1 "2024-06-16T20:04:30Z")

</div>

Hello. I'm new to programming and for some time i have problem with "if" and "else" function. Sometimes when i write my code to practice and when i upload it it gives me error that there is no "if" before "else". I check my code a couple of times but i dont find any easy to find errors(missed bracets or semi colum). At the moment I'm trying to display information on an oled display from IR sensor ( if it detects something or not).

Here is the code:

```arduino
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 
#define SCREEN_ADDRESS 0x3C 
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

 

const int sensor = 18;

void setup() {
   if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS, OLED_RESET)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); 
  }
  // put your setup code here, to run once:
  Serial.begin(115200);
 
}

void loop()
{
   int data = digitalRead(sensor);
  if (data == LOW);
  {
    display.clearDisplay();
    display.setTextColor(WHITE);
    display.setTextSize(2);
    display.setCursor(0,0);
    display.println("Detected");
    display.display();
    
  }
  else
  {
     display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(2);
  display.setCursor(0, 0);
  display.println("Not Detected");
  display.display();
  }
 
}

```

I try to use yt/github or my previous codes as reference for my current one.  
Any tips for the code will be of big help.

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [June 16, 2024, 8:10pm UTC](https://forum.arduino.cc/t/code-gives-me-error-else-without-a-previos-if-when-uploading-the-code/1272447/2 "2024-06-16T20:10:40Z")

</div>

> Any tips for the code will be of big help.

Hello [mrt190](https://forum.arduino.cc/u/mrt190)

Welcome to the worldbest Arduino forum ever 😃

Change

```arduino
if (data == LOW);

```

to

```arduino
if (data == LOW)

```

and try again.

---

<div class="post-metadata">

**Author:** ![beach11](https://avatars.discourse-cdn.com/v4/letter/b/4da419/32.png) [@beach11](https://forum.arduino.cc/u/beach11)\
**Post date:** [June 16, 2024, 8:18pm UTC](https://forum.arduino.cc/t/code-gives-me-error-else-without-a-previos-if-when-uploading-the-code/1272447/3 "2024-06-16T20:18:52Z")

</div>

The ';' is the culprit here. Remove the ;  
Further, since the only difference is the println statement you might want to change it to:

```arduino

void loop()
{
   int data = digitalRead(sensor);
   display.clearDisplay();
   display.setTextColor(WHITE);
   display.setTextSize(2);
   display.setCursor(0,0);
   if (data == LOW) display.println("Detected");
  else display.println("Not Detected");
   display.display();

```

I omitted the curly brackets {}. When only one stament is used after if, you do not need them.

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [June 16, 2024, 8:25pm UTC](https://forum.arduino.cc/t/code-gives-me-error-else-without-a-previos-if-when-uploading-the-code/1272447/4 "2024-06-16T20:25:21Z")

</div>

> [@beach11](#):
>
> ```arduino
> void loop()
> {
> int data = digitalRead(sensor);
> display.clearDisplay();
> display.setTextColor(WHITE);
> display.setTextSize(2);
> display.setCursor(0,0);
> if (data == LOW) display.println("Detected");
> else display.println("Not Detected");
> display.display();
> 
> ```

```arduino
void loop()
{
   bool data = digitalRead(sensor);
   display.clearDisplay();
   display.setTextColor(WHITE);
   display.setTextSize(2);
   if (data)display.println("Not ");
   display.println("Detected");
   display.display();

```

---

<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 13, 2024, 8:26pm UTC](https://forum.arduino.cc/t/code-gives-me-error-else-without-a-previos-if-when-uploading-the-code/1272447/5 "2024-12-13T20:26:14Z")

</div>

Closed on December 13, 2024, 8:26pm UTC

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