Expected unqualified-id before 'else' error in my Arduino code

Hi! Can you please help me with my code? I attach a copy below.

#include <AFMotor.h>  // AFMotor Library https://learn.adafruit.com/adafruit-motor-shield/library-install 
#include <NewPing.h>  // NewPing Library https://github.com/livetronic/Arduino-NewPing
#include <Servo.h>// Servo Library https://github.com/arduino-libraries/Servo.git
#include "pitches.h"

#define TRIG_PIN A0 
#define ECHO_PIN A1 
#define MAX_DISTANCE 200 
#define MAX_SPEED 150 
#define MAX_SPEED_OFFSET 20

NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); 

AF_DCMotor motor1(1, MOTOR12_1KHZ); 
AF_DCMotor motor2(2, MOTOR12_1KHZ);
Servo myservo;   

boolean goesForward=false;
int distance = 100;
int speedSet = 0;
int melody[] = {NOTE_C4, NOTE_G3, NOTE_A3, NOTE_G3, 0 ,NOTE_B3, NOTE_C4};
int noteDurations[] = {4, 8, 8, 4, 4, 4, 4, 4};
int pirSensor = 14;

void setup() {

  myservo.attach(10);  
  myservo.write(115); 
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  pinMode(pirSensor, INPUT);
}

void loop() {
 int distanceR = 0;
 int distanceL =  0;
 int pirState = digitalRead(pirSensor);
 delay(40);
 
 if(distance<=15)
 {
  moveStop();
  delay(100);
  moveBackward();
  delay(300);
  moveStop();
  delay(200);
  distanceR = lookRight();
  delay(200);
  distanceL = lookLeft();
  delay(200);

  if(distanceR>=distanceL)
  {
    turnRight();
    moveStop();
  }else
  {
    turnLeft();
    moveStop();
  }
 }
 {
  moveForward();
 }
 distance = readPing();
}
else if (pirState == 1) {

  for (int thisNote=0; thisNote <15; thisNote++) {

    int noteDuration = 1000 / noteDuration [thisNote];
    tone(15, melody [thisNote], noteDuration);

    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);

    noTone(15);
  }
}

int lookRight()
{
    myservo.write(50); 
    delay(500);
    int distance = readPing();
    delay(100);
    myservo.write(115); 
    return distance;
}

int lookLeft()
{
    myservo.write(170); 
    delay(500);
    int distance = readPing();
    delay(100);
    myservo.write(115); 
    return distance;
    delay(100);
}

int readPing() { 
  delay(70);
  int cm = sonar.ping_cm();
  if(cm==0)
  {
    cm = 250;
  }
  return cm;
}

void moveStop() {
  motor1.run(RELEASE); 
  motor2.run(RELEASE);
  } 
  
void moveForward() {

 if(!goesForward)
  {
    goesForward=true;
    motor1.run(FORWARD);      
    motor2.run(FORWARD);  
   for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) 
   {
    motor1.setSpeed(speedSet);
    motor2.setSpeed(speedSet);
    delay(5);
   }
  }
}

void moveBackward() {
    goesForward=false;
    motor1.run(BACKWARD);      
    motor2.run(BACKWARD);
  for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) 
  {
    motor1.setSpeed(speedSet);
    motor2.setSpeed(speedSet);
    delay(5);
  }
}  

void turnRight() {
  motor1.run(FORWARD);
  motor2.run(BACKWARD);    
  delay(450);
  motor1.run(FORWARD);      
  motor2.run(FORWARD);    
} 
 
void turnLeft() {
  motor1.run(BACKWARD);     
  motor2.run(FORWARD); 
  delay(450);
  motor1.run(FORWARD);     
  motor2.run(FORWARD);

}  
  

I got these error messages

expected unqualified-id before 'else'

Any help appreciated. Thanks.
-Eli

Hi, in the Arduino IDE you can press Ctrl+T, or select "Auto Format" in the Tools menu.

Then go through your sketch and put every space, every comma, every indent, every curly bracket at the right place.

You will see that after closing the loop() function you are trying to add a 'else'.

Hi @compacked

There are "{" and "}" errors in this snippet of your program.

RV mineirin