Greenhouse alert
A greenhouse controller reads the air temperature once a minute, in whole degrees Celsius, and decides what to do. Below 10 degrees it switches the heater on. From 10 to 28 degrees inclusive everything is fine. Above 28 degrees it opens the roof vent.
Read one temperature and print the controller's decision: HEATER ON, OK or VENT OPEN.
Input
One line containing an integer temperature t in degrees Celsius.
Output
One line: HEATER ON if t < 10, OK if 10 <= t <= 28, or VENT OPEN if t > 28.
Example 1
Input
7
Output
HEATER ON
7 is below 10, so the heater comes on.
Example 2
Input
28
Output
OK
28 is the top of the comfortable range and is still inside it.
Constraints
- -50 <= t <= 60
Hints
Hint 1 of 3
Three outcomes need an if, an else if and a final else.
Hint 2 of 3
Once the first test temperature < 10 has failed, you already know the value is at least 10, so the second branch only has to check the upper end.
Hint 3 of 3
Be careful at the boundaries: 10 and 28 are both OK, 9 and 29 are not.
Solution
Show a reference solution and explanation
int temperature = int.Parse(Console.ReadLine()!);
if (temperature < 10)
{
Console.WriteLine("HEATER ON");
}
else if (temperature <= 28)
{
Console.WriteLine("OK");
}
else
{
Console.WriteLine("VENT OPEN");
}
Why it works
An if / else if / else chain is evaluated top to bottom and stops at the first condition that is true. That ordering lets each later branch assume the earlier ones failed: by the time temperature <= 28 runs, the value is already known to be 10 or more, so a single comparison covers the whole middle band. Boundary values are where range bugs hide, which is why the tests include 9, 10, 28 and 29.
Lesson for this exercise: Conditions and Loops in C#