'else' without a previous 'if'

Thanks for the help :). I am still very new to the language.

please help. I am trying to make a variable speed fan with a redboard but the code keeps coming back as 'else' without a previous 'if' error code. code below. help would be appreciated.
if (val == 1);{
digitalWrite(5, HIGH);
digitalWrite(12, LOW);
digitalWrite(16, LOW);

else if (val == 2)
digitalWrite(5, HIGH);
digitalWrite(12, HIGH);
digitalWrite(16, LOW);

else if (val == 3)
digitalWrite(5, HIGH);
digitalWrite(12, HIGH);
digitalWrite(16, HIGH);

else
digitalWrite (5, LOW);
digitalWrite (12, LOW);
digitalWrite (16, LOW);

}

you have brackets and colons in the wrong place! I would advise you touch up on the syntax for the language... :wink:

 if (val == 1){
    digitalWrite(5, HIGH);
    digitalWrite(12, LOW);
    digitalWrite(16, LOW);
   }
  else if (val == 2){
    digitalWrite(5, HIGH);
    digitalWrite(12, HIGH);
    digitalWrite(16, LOW);
 }
  else if (val == 3){
    digitalWrite(5, HIGH);
    digitalWrite(12, HIGH);
    digitalWrite(16, HIGH);
 }
  else{
    digitalWrite (5, LOW);
    digitalWrite (12, LOW);
    digitalWrite (16, LOW);
 
  }

hope that helps...

 if (val == 1);

Remove the semicolon

It is the only code that is executed conditionally when the test returns true

A semicolon by itself is a null statement, rarely useful, although it can be used like this:

  if (condition)
    ;  // do nothing
  else
    do_something() ;

Which is a way of avoiding inverting the condition, its perfectly reasonable to do this instead:

  if (!(condition))
    do_something() ;