Ignoring IF statment.

hello i am having troubble with my code (or tutorial code to be precise).
i have written it excaktly and tried several methods but still the arduino ignores the IF statment and just goes straight to blinking and spamming "Helloworld!" through the serial.

im slowly losing my mind over this.

  int val;
  int ledpin=13;
  
void setup()
{
  Serial.begin(9600);
  pinMode(ledpin,OUTPUT);
}

void loop()
{
  val=Serial.read();
  if(val=='F');
  {
    digitalWrite(ledpin,HIGH);
    delay(500);
    digitalWrite(ledpin,LOW);
    delay(500);
    Serial.println("Helloworld!");
  }
}
if(val=='F'); // lose the ";"
if(val=='F');

The trailing semi-colon is your problem. It is the only piece of code executed if the test is true.
Check out the Arduino reference for the syntax of the if command.

You must never call Serial.read() if you haven't perviously called Serial.available() to
check there is at least one character available to read.

You want something more like:

void loop()
{
  if (Serial.available () > 0)
  {
    int val=Serial.read();  // val shouldn't be global, its local
    if (val == 'F')     // NO SEMICOLON HERE
    {
      ...
    }
  }
  ...

thank you for the answers, could you point me somewhere i can read up on how to properly use the serial? since i am using a tutorial to learn and this is what it told me to do so i'm copycating while i learn how the code works.

You must never call Serial.read() if you haven't perviously called Serial.available() to check there is at least one character available to read.

I would disagree with that statement. Serial.read will return -1 if there is no data, which will certainly not be 'F'...

Well after removing the ";" the code works perfectly, and it's easy to miss that small detail but still get a big unwanted result. need to keep my eyes more open when checking my code but i guess that will come efter some more experience and screaming with frustration at my collection och elektronik components a few times.

i thank you for you help with this.

OK, that was a bit strong, but experience on these forums shows neophytes never
test the result of read() for -1, but many use available () - although not always
carefully enough.

available() is more general, as in

  if (Serial.available () >= 2)
  {
    byte hibyte = Serial.read () ;
    byte lobyte = Serial.read () ;
    ....
  }

So just use available() for availability testing - only one mechanism needs to be learnt.

Personally I'd have made read() blocking, since there is already available() available,
it would have meant less confusion for beginners.