Hello!
A simple question. Let's say I have a range of numbers, 1-10. If the variable x is not that range of numbers, a while loop continues. How would I do this in a small bit of code? I would think to do some sort of array but I can't seem to find anything.
This is the brute force version of what I want:
while (x != 1 and x != 2 and x != 3 ... x != 8 and x!= 9 and x != 10) {
//do stuff
}
I would consider putting the numbers to be checked against in an array theNumbers[]: in the more general sense they then don't have to be in a range 1-10 but any numbers 26, 5, 18, 9, etc.
Then have a flag called say numberNotMatched, initially true, and in a for() check the variable against each element and if there's a match, set the flag false.
So when the for() is finished, if there was no match your flag is still true, but false if any number did match.
And then if the flag is true do stuff, otherwise do nothing or other stuff.
pontiacbandit:
I would consider putting the numbers to be checked against in an array theNumbers[]: in the more general sense they then don't have to be in a range 1-10 but any numbers 26, 5, 18, 9, etc.
It actually depends on the needs. Based on the example that OP provided, the compare against two limits is faster.
With your approach, if you keep the numbers in sequence in the array, you can use a binary search. It's on average the faster approach opposed to iterating through the array.