expected ';' before '{' token

I get an expected ';' before '{' token error when trying to compile my code.
It appears on the first line of the following section

else(distance>=40){
 
  StepsRequired  = 0;   
  steppermotor.setSpeed(700);  
  steppermotor.step(StepsRequired);
}

I aim to have a stepper motor stop if distance value received from an ultrasonic sensor is more than 40cm, move clockwise between 30-40 cm, and counter clockwise 0-30cm.

The problem seems to lie with else, as I can compile fine if this is removed.
Any suggestions?

Problem occurs near end of code:

/*

#include <Stepper.h>
 
// STEPPER CONSTANTS
 
// Number of steps per internal motor revolution 
const float STEPS_PER_REV = 32; 
 
//  Amount of Gear Reduction
const float GEAR_RED = 64;
 
// Number of steps per geared output rotation
const float STEPS_PER_OUT_REV = STEPS_PER_REV * GEAR_RED;
 
// Define Variables
 
// Number of Steps Required
int StepsRequired;
 
 
Stepper steppermotor(STEPS_PER_REV, 8, 10, 9, 11);


// SENSOR CONSTANTS

// defines pins numbers
const int trigPin = 12;
const int echoPin = 13;

// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}


void loop() {
{
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}


 
if(distance<=30){
 
  StepsRequired  =  - STEPS_PER_OUT_REV / 20;   
  steppermotor.setSpeed(700);  
  steppermotor.step(StepsRequired);
}

else if(distance>30){
  
  StepsRequired  = STEPS_PER_OUT_REV / 20;   
  steppermotor.setSpeed(700);  
  steppermotor.step(StepsRequired);
}

else(distance>=40){
 
  StepsRequired  = 0;   
  steppermotor.setSpeed(700);  
  steppermotor.step(StepsRequired);
}
  
}

The code you posted wouldn't cause that error because it's just a bit unterminated comment. Please make sure to post the same code to the forum as the code you're compiling.

As for the error, please take a little time to study the reference page for else to understand how to use it correctly:

You have a instruction after else
it must always be in the form
"} else {"

you need to be using "else if"

Cheetor:
You have a instruction after else
it must always be in the form
"} else {"

you need to be using "else if"

Thanks!
Replacing else with else if solved the error.
The result still wasn't what I wanted, but it worked after I changed the order of my code.

The following code works.

#include <Stepper.h>
 
// Define Constants
 
// Number of steps per internal motor revolution 
const float STEPS_PER_REV = 32; 
 
//  Amount of Gear Reduction
const float GEAR_RED = 64;
 
// Number of steps per geared output rotation
const float STEPS_PER_OUT_REV = STEPS_PER_REV * GEAR_RED;
 
// Define Variables
 
// Number of Steps Required
int StepsRequired;
 
 
Stepper steppermotor(STEPS_PER_REV, 8, 10, 9, 11);

// defines pins numbers
const int trigPin = 12;
const int echoPin = 13;
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}

void loop() {
{
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}


 
if(distance>=40){
 
  StepsRequired  = 0;   
  steppermotor.setSpeed(700);  
  steppermotor.step(StepsRequired);
}

else if(distance>30){
  
  StepsRequired  = STEPS_PER_OUT_REV / 20;   
  steppermotor.setSpeed(700);  
  steppermotor.step(StepsRequired);
}

else if(distance<=30){
 
  StepsRequired  =  - STEPS_PER_OUT_REV / 20;   
  steppermotor.setSpeed(700);  
  steppermotor.step(StepsRequired);
}
  
}