I'd like to test a variable ("userChoice") for the numeric values 0-32 and 99
To enter a range to use as a criterion, type *, enter the range reference normally, then after the range reference but before the right parenthesis, type =", then the value to match, then ".
The multiple IF conditions in Excel are IF statements contained within another IF statement. They are used to test multiple conditions simultaneously and return distinct values. The additional IF statements can be included in the “value if true” and “value if false” arguments of a standard IF formula.
if((userChoice >= 0 && userChoice <= 32) || userChoice == 99)
{
// do stuff
}
Just to add a different kind of thinking, when I have range tests I like to use the Contains method of List<T>. In your case it may seem contrived but it would look something like:
List<int> options = new List<int>(Enumerable.Range(0, 33));
options.Add(99);
if(options.Contains(userChoice)){
// something interesting
}
If you were operating in the simple range, it would look much cleaner:
if(Enumerable.Range(0, 33).Contains(userChoice)){
// something interesting
}
What's nice about this is that is works superbly well with testing a range of strings and other types without having to write || over and over again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With