IF and OR
BlitzMax Forums/BlitzMax Beginners Area/IF and OR
| ||
Hi I'm wondering how I can construct an IF statement so that it is true if it equals one of two values given. For example I thought this would work but its obviously not evaluating what I would expect it to. Obviously I know I could put : If ( a = 6 ) or ( a = 8 ) but I just wanted to make my code a little more readable as I have quite a few long if statements that are a little difficult to digest. Is this possible? thanks |
| ||
No this is not possible. Or is a binary operator that evaluates to {true,false}. Your version would need some kind of list comparision. You could get this behavior with if not instr(["6","8"],string(a)) if I am not complete wrong |
| ||
Ok thanks for your response |
| ||
If your list is that much long and it really makes sense to shorten the test, you can just doSelect a Case 1,2,3,4,5 Print "A is 1 or 2 or 3 or 4 or 5" End Select |
| ||
If (a=6) or (a=8) |
| ||
thats the one he didn't like ;-) If you make the numbers more usefull like all the same mod value or with bitflagging, then you could the whole stuff really cut down to 1 test and 1 calculation in worst case. |
| ||
Of course there's also: Select a case 1,2,3,4 Print "A is 1,2,3 or 4" case 5 print "A is 5" case 6,8 Print "A is 6 or 8" end select Which is a lot cleaner if you have to check for a whole bunch of conditions... But for just two or so, the if (a=6 or (a=8) is probably the easiest. |