Add security lag time not to fry heater connected to relay

Hi, I have a temperature controlled relay, I would like to add some security so I wont fry the heater that is connected to the relay by turning it on or off too often.
For example if the relay is set to turn on if the temp is less than 20C I actually want to wait until it has dropped to 19.5 (0.5C buffer temp) before it actually turn on. I think my below pseudo code will work (not tested), but it seem clumsy using nested if statements, I would learn to write better code. Can it be made neater/smarter, often I feel i make it harder than it need to be. Any suggestions or comments?

Some pseudo code
// Check temp every 15 second

void checkTemp()
If (temp < 20C turn on relay)
  {
    Actually only turn relay on when temp has dropped to 19.5C
    if(temp < 19.5) 
      {
        relayOn();
      } else {
        // Do nothing - wait until temp is 19.5 - relayOff()
      }
else {
  relayOff();
}

A difference between turn on temperature and turn off is called hysteresis.

if( temp < 19.5)  // set turn on
{
  relayOn();
{
else if( temp > 21.5) // set turn off
{
  relayOff();
}

As groundFungus notes, hysteresis is important to prevent "chattering" at a single switchpoint in systems like this.

You can also add temperature sensor checks, a maximum run time and cool-off time etc...

#define SET_POINT   (float)20.0
#define LOW_HYST    (float)-0.50
#define HIGH_HYST   (float)+0.75

#define HTR_OFF     0
#define HTR_ON      1
#define MAX_HEATER_ON   600000L     //10-min cont. run time max
#define HTR_PAUSE_TIME  600000L     //10-min rest after max-out

//if the sensor is shorted or open, what does it read?
#define ERROR_LOW   (float)-40.0
#define ERROR_HIGH  (float)+80.0

void setup()
{
    .
    .
    .
    relayOff();
    .
    .
    .
}

void loop()
{
    .
    .
    .
    checkTemp();
    .
    .
    .    
}

void checkTemp()
{
    unsigned long
        timeNow;
    static unsigned long
        timerHeater;
    static byte
        stateHeater = HTR_OFF;

    //read the temperature and filter it
    fTemp = getTemp();
    filtTemp = filterTemp( fTemp ); //could be a lag filter (e.g.)

    //check for sensor errors
    if( (filtTemp <= ERROR_LOW) || (filtTemp >= ERROR_HIGH) )
    {
        //temp sensor may be faulty
        //force heater off and let user know somehow
        lightErrorLED();
        relayOff();
        return;
    }

    switch( stateHeater )
    {
        case    HTR_OFF:
            if( filtTemp <= (SET_POINT - LOW_HYST ) )
            {
                //lower target temp; turn heater on
                relayOn();
                timerHeater = millis();
                stateHeater = HTR_ON:  
            }//if
            
        break;

        case    HTR_ON:
            if( (millis()-timerHeaterOn) > MAX_HEATER_ON )
            {
                //heater has been running too long...let it cool
                relayOff();
                timerHeater = millis();
                stateHeater = HTR_PAUSE;
                
            }//if
            else if( filtTemp >= (SET_POINT + HIGH_HYST ) )
            {
                //upper target temp reached; turn it off
                relayOff();
                stateHeater = HTR_OFF;
                
            }//if
        break;

        case    HTR_PAUSE:
            //if heater was on too long, give it a chance to
            //cool before allowing it to run again
            if( (millis() - timerHeater) < HTR_PAUSE_TIME )
                return;

            //after pause, return to the normal state machine
            stateHeater = HTR_OFF;
            
        break;
    }//switch
    
}//checkTemp

Thanks guys that was really helpful! Hysteresis, think its my new work of the day!