error: expected unqualified-id before 'if'

hi i have a problem with a error that says: "error: expected unqualified-id before 'if'" i have checked it sevaral times and i dont find any errors? can you help me

void setup()
{
Serial.begin(9600);
}

void loop()
{
analogWrite(4, 200);
analogWrite(5, 200);

long duration, cm;

pinMode(2, OUTPUT);
digitalWrite(2, LOW);
delayMicroseconds(2);
digitalWrite(2, HIGH);
delayMicroseconds(5);
digitalWrite(2, LOW);

duration = pulseIn(2, HIGH);

cm = microsecondsToCentimeters(duration);

Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(100);
}

long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}

if (cm < 10)
{
analogWrite(4, 0);
analogWrite(5, 0);
}

Project_control:38: error: expected unqualified-id before 'if'

Your if statement needs to be inside a function. Presumably inside loop(), just after:

  cm = microsecondsToCentimeters(duration);

Please remember to use ... tags when posting code. See point 6 in http://arduino.cc/forum/index.php/topic,97455.0.html

thanks it worked XD