[ERROR] expected unqualified-id on "if" and "else"

Good evening. I've been receiving an error, and i dont know how to fix it. I'm still new to programming. Thanks.

sketch_jan15a:18:1: error: expected unqualified-id before 'if'
if (buttonState == HIGH)
sketch_jan15a:22:1: error: expected unqualified-id before 'else'
exit status 1
expected unqualified-id before 'if'

the code:

const int sensorPin = 4;
const int buttonPin = 5;
const int motorPin = 9;
int sensorState = 0; 
int buttonState = 0; 
void setup() {
 pinMode(sensorPin, INPUT);
 pinMode(buttonPin, INPUT);
 pinMode(motorPin, OUTPUT);


void loop(){
sensorState = digitalRead(sensorPin);
buttonState = digitalRead(buttonPin);

if (buttonState == HIGH)
  digitalWrite (motorPin, HIGH);
  digitalWrite (motorPin, LOW);

the errors are at lines 18 and 22. Thanks.

your if/else is outside the loop function.

All code needs to be within functions.

if you make sure you indented the code in the IDE (that's done by pressing ctrlT on a PC or cmdT on a Mac) you would see where the loop function starts and ends.

thank you so much!! i genuinely feel dumb cause i forgot about that but thanks again!