# Expected '}' before 'else'

**URL:** <https://forum.arduino.cc/t/expected-before-else/646069>\
**Category:** Programming\
**Created:** [April 2, 2020, 11:20am UTC](https://forum.arduino.cc/t/expected-before-else/646069 "2020-04-02T11:20:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![peekaboo\_dgm](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/peekaboo_dgm/32/522251_2.png) [@peekaboo\_dgm](https://forum.arduino.cc/u/peekaboo_dgm)\
**Post date:** [April 2, 2020, 11:20am UTC](https://forum.arduino.cc/t/expected-before-else/646069/1 "2020-04-02T11:20:37Z")

</div>

Hi,

I'm having a bit of trouble with this code - I am trying to make lights on pins 2 and 3 light up when the Arduino is tilted beyond flat.

There is apparently supposed to be a '}' before the 'else' on line 30 (within voidLoop()), although I am very new to C++ and I'm not sure what to do.

Thanks for any help! 🙂

```cpp
#include <Arduino_LSM9DS1.h>
int light_one = 2;
int light_two = 3;

void setup()
{
  pinMode(light_one, OUTPUT);
  pinMode(light_two, OUTPUT);

  if (!IMU.begin())
  {
    Serial.println("Failed to initialize IMU!");
    exit(1);
  }
}

void loop()
{
  float x, y, z, delta = 0.05;

  if (IMU.accelerationAvailable())
  {
    IMU.readAcceleration(x, y, z);

    if (y <= delta && y >= -delta)
      Serial.println("flat");
    pinMode(light_one, LOW);
    pinMode(light_two, LOW);

    else if (y > delta && y < 1 - delta)
      Serial.println("tilted to the left");
    pinMode(light_one, HIGH);
    pinMode(light_two, HIGH);

    else if (y >= 1 - delta)
      Serial.println("left");
    pinMode(light_one, HIGH);
    pinMode(light_two, HIGH);

    else if (y < -delta && y > delta - 1)
      Serial.println("tilted to the right");
    pinMode(light_one, HIGH);
    pinMode(light_two, HIGH);

    else
      Serial.println("right");
    pinMode(light_one, HIGH);
    pinMode(light_two, HIGH);
  }

  delay(150);
}

```

---

<div class="post-metadata">

**Author:** ![MarkT](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@MarkT](https://forum.arduino.cc/u/MarkT)\
**Post date:** [April 2, 2020, 11:29am UTC](https://forum.arduino.cc/t/expected-before-else/646069/2 "2020-04-02T11:29:20Z")

</div>

Perhaps read up on the if-statement syntax and all will become clear?

if ( ) else   
or  
if ( )

where a statement is a single statement.

A block statement counts as a single statement.

Or simply put you need braces round the then or else statements unless they are single statements.

---

<div class="post-metadata">

**Author:** ![noiasca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/noiasca/32/1126910_2.png) [@noiasca](https://forum.arduino.cc/u/noiasca)\
**Post date:** [April 2, 2020, 11:29am UTC](https://forum.arduino.cc/t/expected-before-else/646069/3 "2020-04-02T11:29:29Z")

</div>

> There is apparently supposed to be a '}' before the 'else' on line 30 (within voidLoop()), although I am very new to C++ and I'm not sure what to do.

do what the compiler tells you: add a } before the else.

Press CTRL-T in the Arduino IDE - you might see the problem better then...

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [April 2, 2020, 11:29am UTC](https://forum.arduino.cc/t/expected-before-else/646069/4 "2020-04-02T11:29:43Z")

</div>

```cpp
if (y <= delta && y >= -delta)

```

Are you missing a { there?

---

<div class="post-metadata">

**Author:** ![peekaboo\_dgm](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/peekaboo_dgm/32/522251_2.png) [@peekaboo\_dgm](https://forum.arduino.cc/u/peekaboo_dgm)\
**Post date:** [April 2, 2020, 11:52am UTC](https://forum.arduino.cc/t/expected-before-else/646069/5 "2020-04-02T11:52:46Z")

</div>

Thank you for the responses - it's my first time with Arduino so I'm not yet clued up on the statement syntax!

I hadn't added '{}' around the if, else if and else statements.

Thanks!

---

<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 6, 2021, 12:25am UTC](https://forum.arduino.cc/t/expected-before-else/646069/6 "2021-05-06T00:25:22Z")

</div>


