'Val' does not name a type

'Val' doesn't name the type, can you tell me how to fix it? Here is the error text:

Arduino: 1.8.5 (Windows 10), Board:"Arduino/Genuine Uno"

AFMotor_Constant Speed 3:50: error: 'val' does not name a type

exit status 1
'val' does not name a type

This report will have more information with
enabled file -> Settings ->
"Show verbose output during compilation"

And the text of the sketch :

#define motorAgo 11       
#define motorBgo 10      
#define motorAbac 9       
#define motorBbac 6
      
int command;            

void setup() {
  pinMode (motorAgo, OUTPUT);
  pinMode (motorBgo, OUTPUT);
  pinMode (motorAbac, OUTPUT);
  pinMode (motorBbac, OUTPUT);
}


void goAhead() { // едем вперед
  digitalWrite (motorAgo, HIGH);
  digitalWrite (motorBgo, HIGH);
  digitalWrite (motorAbac, LOW);
  digitalWrite (motorBbac, LOW);
}

void goBack() {// едем назад
  digitalWrite (motorAgo, LOW);
  digitalWrite (motorBgo, LOW);
  digitalWrite (motorAbac, HIGH);
  digitalWrite (motorBbac, HIGH);
}

void stopRobot() { // останавливаемся
  digitalWrite (motorAgo, LOW);
  digitalWrite (motorBgo, LOW);
  digitalWrite (motorAbac, LOW);
  digitalWrite (motorBbac, LOW);
}

void goLeft() { // поворачиваем налево
  digitalWrite (motorAgo, HIGH);
  digitalWrite (motorBgo, LOW);
  digitalWrite (motorBbac, HIGH);
  digitalWrite (motorAbac, LOW);
}

void goRight() { // поворачиваем направо
  digitalWrite (motorAgo, LOW);
  digitalWrite (motorBgo, HIGH);
  digitalWrite (motorBbac, LOW);
  digitalWrite (motorAbac, HIGH);
}
val = Serial.read() //The error is here, how to fix it?
switch (command) {
case 'f': goAhead(); break;
case 'b': goBack(); break;
case 's': stopRobot(); break;
case 'l': goLeft(); break;
case 'r': goRight(); break;
}

(deleted)

val = Serial.read() not in any function, no semicolon, no datatype given for "val"

A line something like 'void loop() {' somewhere in your code might also help.

Steve

Thank you advice, but now I have another mistake in the same place. Here is the error text:

Arduino: 1.8.5 (Windows 10), Board:"Arduino/Genuine Uno"

AFMotor_Constant Speed 4:51: error: expected unqualified-id before 'if'

exit status 1
expected unqualified-id before 'if'

This report will have more information with
enabled file -> Settings ->
"Show verbose output during compilation"

.

And text sketch:

#define motorAgo 11
#define motorBgo 10
#define motorAbac 9
#define motorBbac 6

int command;

void setup() {
  pinMode (motorAgo, OUTPUT);
  pinMode (motorBgo, OUTPUT);
  pinMode (motorAbac, OUTPUT);
  pinMode (motorBbac, OUTPUT);
}


void goAhead() { // едем вперед
  digitalWrite (motorAgo, HIGH);
  digitalWrite (motorBgo, HIGH);
  digitalWrite (motorAbac, LOW);
  digitalWrite (motorBbac, LOW);
}

void goBack() {// едем назад
  digitalWrite (motorAgo, LOW);
  digitalWrite (motorBgo, LOW);
  digitalWrite (motorAbac, HIGH);
  digitalWrite (motorBbac, HIGH);
}

void stopRobot() { // останавливаемся
  digitalWrite (motorAgo, LOW);
  digitalWrite (motorBgo, LOW);
  digitalWrite (motorAbac, LOW);
  digitalWrite (motorBbac, LOW);
}

void goLeft() { // поворачиваем налево
  digitalWrite (motorAgo, HIGH);
  digitalWrite (motorBgo, LOW);
  digitalWrite (motorBbac, HIGH);
  digitalWrite (motorAbac, LOW);
}

void goRight() { // поворачиваем направо
  digitalWrite (motorAgo, LOW);
  digitalWrite (motorBgo, HIGH);
  digitalWrite (motorBbac, LOW);
  digitalWrite (motorAbac, HIGH);
}

if (Serial.available() > 0) // The error is here
{
  char command = Serial.read();
  switch (command) {
    case 'f': goAhead(); break;
    case 'b': goBack(); break;
    case 's': stopRobot(); break;
    case 'l': goLeft(); break;
    case 'r': goRight(); break;
  }
}

if (Serial.available() > 0) // The error is hereThat line of code is not in a function

You can see this clearly if you Auto format the code in the IDE

void goRight()   // поворачиваем направо
{
  digitalWrite (motorAgo, LOW);
  digitalWrite (motorBgo, HIGH);
  digitalWrite (motorBbac, LOW);
  digitalWrite (motorAbac, HIGH);
}

if (Serial.available() > 0) // The error is here
{
  char command = Serial.read();
  switch (command)
  {
    case 'f':
      goAhead();
//etc, etc