I am messing around with C# and am making a prototype GUI (with no game attached, just messing around with buttons and button colors). I’m running into an error:
private void temperValue_Load(object sender, EventArgs e)
{
int temperInt = 23;
temperInt = Convert.ToInt32(temperValue.Text);
if (temperInt >= 70)
{
temperButton.BackColor = System.Drawing.Color.Red;
}
else if (temperInt >= 40 & <= 69)
{
temperButton.BackColor = System.Drawing.Color.DarkOrange;
}
}
On the “else if” line, I have an error from both the “<=” and the “69)”. The “<=” error is “Invalid expression term ‘<='”, and the four errors for the “69)” is “) expected”, “Invalid expression term ‘)'”, and two “; expected” errors.
There are no variables outside of this snippet of code that are affecting this code. Every variable called is defined inside the snippet.
(For anyone curious, “temper” stands for “Temperature”)
You cannot take shortcuts in your boolean conditions like that.
Must instead be written as:
Note that when making boolean comparisons, you usually want to use the double ampersand
&&. This causes short-circuiting (only evaluate both sides if the left side succeeds) which is usually what is wanted. And as I said, you need to include thetemperIntidentifier both times — you can’t say “where some variable is greater than one value and less than another” like in a SQLBETWEENclause.Update: Fixed answer per Eric’s suggestion.