Turn on and let LED stay on if value is over threshold in main while-loop
This is exactly what you do - you evaluate if/else at every cycle. Your code is basically ok, it just has one flaw - if the temperature is near the threshold your LED will blink rapidly as temperature readings will be above 40 and below 40 a little bit. You might not want that. To avoid this you have to implement hysteresis - have some margin temperature has to overcome to either turn LED on or off. Something like this:

Threshold = 40 //Celsius
Hysteresis = 1
While true
    Temp = Read_temp_from_sensor()

    If Temp &gt; Threshold + Hysteresis
         Turn on LED
    Else If Temp &lt; Threshold - Hysteresis
         Turn off LED


You might want to consider sleeping the microcontroller in between loop cycles, using interrupts form the watchdog to wake it up in equal time intervals.