If and Else if problem

I would like to program it as follows:

Once turn on: (Heater ON)
temp reaches 50°C: turn off and then turn on only when it cools till 40°C

I would like to know how to turn the hearer on only when it reaches 40°C.
My code is as follows:

void loop()
{
if (temp > 50)
{
digitalWrite(HeaterPin, LOW);
Serial.println("Heater OFF");
}

else
{
digitalWrite(HeaterPin, HIGH);
Serial.println("Heater ON");
}

Change your ‘else’ to ‘if (temp < 40)’.

Thank you johnwasser.

I got another question,

void loop()
{
    if (temp > 50)
    { 
          digitalWrite(HeaterPin, LOW); 
          Serial.println("Heater OFF");
     }

    if (temp < 40)
    {
          digitalWrite(HeaterPin, HIGH); 
          Serial.println("Heater ON");
     }

Would the Heater still be able to reach temp ranging from 40-50°C given the if conditions?

Your code doesn’t compile, so the question is moot.

shaunthc:
I (have) got another question.
Would the Heater still be able to reach temp ranging from 40-50°C given the if conditions?

Unless you have some hardware mistake like the heater power being too low or the part of the sketch you didn't show has a mistake like failing to read the temperature each time through loop() the temperature should mostly stay in the 40 to 50 range. There will be some overshoot when heating and some undershoot when cooling but it shouldn't be bad.