1

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")

h3half
  • 226
  • 1
  • 3
  • 16
  • Isn't checking for `<= 69` redundant? If it's made it to the `else` then it must be 69 or smaller. – Jeremy Wiggins Jan 19 '12 at 04:02
  • 2
    In addition to the correct answers: it is also more clear to the reader if you write the code like this: `if (40 <= x && x <=69)` because that emphasizes lexically that x is *between* them. – Eric Lippert Jan 19 '12 at 04:31

5 Answers5

7

You cannot take shortcuts in your boolean conditions like that.

else if (temperInt >= 40 & <= 69)

Must instead be written as:

else if (temperInt >= 40 && temperInt <= 69)

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 the temperInt identifier both times -- you can't say "where some variable is greater than one value and less than another" like in a SQL BETWEEN clause.

Update: Fixed answer per Eric's suggestion.

K Scandrett
  • 16,390
  • 4
  • 40
  • 65
Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
  • Might I ask: What is the difference between the two? (I mean what does the extra ampersand do. I know it's there) – h3half Jan 19 '12 at 03:45
  • && is logical AND and & is a bitwise AND – Keith Nicholas Jan 19 '12 at 03:47
  • 1
    Two ampersands is for comparing boolean values. One ampersand is for bitwise manipulation. (Same is true for the `|` operator) – Kirk Woll Jan 19 '12 at 03:48
  • 3
    @h3half: The last paragraph of this answer is incorrect. It is *not* the case that `&` and `|` may not be used for Booleans. It is perfectly legal; try it! The difference is that `|` always evaluates both halves. The `||` skips evaluating the right half if the left half is true. Similarly for `&&` -- if the left half is false then the right half is not evaluated. But it is simply false to say that the operators do not apply to bools; they certainly do. – Eric Lippert Jan 19 '12 at 04:26
  • @KeithNicholas and Kirk Wolf: I encourage you to read section 7.11.3 of the C# 4 specification. – Eric Lippert Jan 19 '12 at 04:34
  • @Eric, I knew that once upon a time... Thanks for the correction. – Kirk Woll Jan 19 '12 at 04:35
  • '... you can't say "where some variable is greater than one value and less than another" like in a SQL BETWEEN clause' - sure you can (see my answer). But it's like trying to kill a mosquito with a thermonuke :-) – paxdiablo Jan 19 '12 at 06:12
3
if (temperInt >= 40 & <= 69) ...

is not valid C#. Computer languages are a little more restrictive than natural languages. You should use:

if (temperInt >= 40 && temperInt <= 69) ...

(you'll notice I'm also using the logical && operator rather than the bitwise & operator - the former is for truth values, the latter usually for bit manipulation, see this answer for details).

There's another alternative, the use of extension methods:

bool IsBetween (this int me, int lower, int upper) { 
    return (me >= lower) && (me <= upper); 
}

if (temperInt.IsBetween (40, 69)) ...

which is closer to natural language, but that's probably overkill for this case.

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
2

you probablly meant temperInt >= 40 && temperInt <= 69

Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
2
else if (temperInt >= 40 & <= 69)

Should be:

else if (temperInt >= 40 && temperInt <= 69)

You need to include the variable in both parts of the statement, and & is a bitwise AND whereas && is a logical AND which is what you want in this case.

Matt Lacey
  • 8,227
  • 35
  • 58
2

There are a few errors in the given code.

 else if (temperInt >= 40 & <= 69)
    {
        temperButton.BackColor = System.Drawing.Color.DarkOrange;
    }

This should actually read

 else if (temperInt >= 40 && temperInt <= 69)
    {
        temperButton.BackColor = System.Drawing.Color.DarkOrange;
    }

The && is the logical AND operator in C# and not the '&'. Also the LHS part need to be used in all equality comparisons and not chained like your code sample.

IUnknown
  • 888
  • 7
  • 18