# Compilation error: 'else' without a previous 'if'

**URL:** <https://forum.arduino.cc/t/compilation-error-else-without-a-previous-if/1383539>\
**Category:** Programming\
**Created:** [May 22, 2025, 9:05am UTC](https://forum.arduino.cc/t/compilation-error-else-without-a-previous-if/1383539 "2025-05-22T09:05:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Declan](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/declan/32/1126393_2.png) [@Declan](https://forum.arduino.cc/u/Declan)\
**Post date:** [May 22, 2025, 9:05am UTC](https://forum.arduino.cc/t/compilation-error-else-without-a-previous-if/1383539/1 "2025-05-22T09:05:49Z")

</div>

I get the above compile error.  
I have looked at the forum topics, but cannot see where I am going wrong.

```arduino
void loop() {
  uint8_t selectedMenuItem = lcd.showMenu(menu, menuLen, 1);

  if (selectedMenuItem == mkLoadLabels)
    lcd.print("Loading Labels");
  // loadLabels(); <--- IF I UNCOMMENT THIS LINE, I GET THE COMPILE ERROR

  else if (selectedMenuItem == mkRun)
    lcd.print("Start Labeller");
  else if (selectedMenuItem == mkSpeed)
    lcd.print("Speed selected");
  else if (selectedMenuItem == mkSpeed1)
    lcd.print("Slow selected");
  else if (selectedMenuItem == mkSpeed2)
    lcd.print("Medium selected");
  else if (selectedMenuItem == mkSpeed3)
    lcd.print("Fast selected");

  else if (selectedMenuItem == mkBack)
    lcd.print("Exit selected");
  while (lcd.getEncoderState() == eNone);
}

```

---

<div class="post-metadata">

**Author:** ![6v6gt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/6v6gt/32/225488_2.png) [@6v6gt](https://forum.arduino.cc/u/6v6gt)\
**Post date:** [May 22, 2025, 9:09am UTC](https://forum.arduino.cc/t/compilation-error-else-without-a-previous-if/1383539/2 "2025-05-22T09:09:21Z")

</div>

```arduino
if (selectedMenuItem == mkLoadLabels) {
    lcd.print("Loading Labels");
    loadLabels(); // <--- IF I UNCOMMENT THIS LINE, I GET THE COMPILE ERROR
  } 
else if (selectedMenuItem == mkRun)
. . .

```

---

<div class="post-metadata">

**Author:** ![Declan](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/declan/32/1126393_2.png) [@Declan](https://forum.arduino.cc/u/Declan)\
**Post date:** [May 22, 2025, 9:24am UTC](https://forum.arduino.cc/t/compilation-error-else-without-a-previous-if/1383539/3 "2025-05-22T09:24:19Z")

</div>

Many thanks

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [May 22, 2025, 9:32am UTC](https://forum.arduino.cc/t/compilation-error-else-without-a-previous-if/1383539/4 "2025-05-22T09:32:41Z")

</div>

Did you understand what the problem was ?

In C++, indentation serves only to improve code readability; unlike Python, it does not define the grouping of statements. The structure of an if/else statement looks like this:

```arduino
if (condition)
  statement_if_true
else
  statement_if_false

```

Here, each branch executes a single statement, so if you want to do

```arduino
if (selectedMenuItem == mkSpeed1)
    lcd.print("Slow selected");

```

then you are fine as ` lcd.print("Slow selected");` is a single statement.

But if you want to execute multiple statements as a group within an if or else like

```arduino
if (selectedMenuItem == mkLoadLabels) 
    lcd.print("Loading Labels");
    loadLabels(); 

```

it does not work as you have two statements. You need to enclose them in curly braces `{}`, creating what is known as a compound statement.

```arduino
{ // this is a compound statement (so just one statement composed of multiple statements)
    lcd.print("Loading Labels");
    loadLabels(); 
}

```

This way, the if or else still contains a single statement—the compound one—while grouping multiple commands together.

Makes sense?

---

<div class="post-metadata">

**Author:** ![Declan](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/declan/32/1126393_2.png) [@Declan](https://forum.arduino.cc/u/Declan)\
**Post date:** [May 26, 2025, 11:22am UTC](https://forum.arduino.cc/t/compilation-error-else-without-a-previous-if/1383539/5 "2025-05-26T11:22:11Z")

</div>

Yes, many 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:** [November 22, 2025, 11:22am UTC](https://forum.arduino.cc/t/compilation-error-else-without-a-previous-if/1383539/6 "2025-11-22T11:22:42Z")

</div>

Closed on November 22, 2025, 11:22am UTC

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