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

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

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);
}
if (selectedMenuItem == mkLoadLabels) {
    lcd.print("Loading Labels");
    loadLabels();  // <--- IF I UNCOMMENT THIS LINE, I GET THE COMPILE ERROR
  } 
else if (selectedMenuItem == mkRun)
. . .

Many thanks

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:

if (condition)
  statement_if_true
else
  statement_if_false

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

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

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.

{ // 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?

Yes, many thanks