Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Evaluate if one variable has several values
#1
Hi all

Instead of...
Code:
IF var = 1 OR var = 2 OR var = 3 OR var = 4 THEN

... is there any way to achieve the same in a more efficient way? Something like that:

Code:
IF var = (1 OR 2 OR 3 OR 4) THEN

Thanks in advance and cheers
Reply
#2
oblo Wrote:Hi all

Instead of...
Code:
IF var = 1 OR var = 2 OR var = 3 OR var = 4 THEN

... is there any way to achieve the same in a more efficient way? Something like that:

Code:
IF var = (1 OR 2 OR 3 OR 4) THEN

Thanks in advance and cheers

No, unfortunately not. OR joins two boolean expressions, and if you do "1 OR 2" that evaluates to "TRUE OR TRUE" = TRUE which is not what you are looking for.

While in English that might sound sensible, no programming language allows that shortcut with booleans. Some languages allow a switch command to test for multiple IF cases, but it's not available in ZXB.

You need to test (var=1) OR (var=2) to have the right true false values either side of the OR. You might be able to have something like "IF INT ver >0 AND INT var < 5 THEN" if the numbers are in a range, though.
Reply
#3
britlion Wrote:
oblo Wrote:Hi all

Instead of...
Code:
IF var = 1 OR var = 2 OR var = 3 OR var = 4 THEN

... is there any way to achieve the same in a more efficient way? Something like that:

Code:
IF var = (1 OR 2 OR 3 OR 4) THEN

Thanks in advance and cheers

No, unfortunately not. OR joins two boolean expressions, and if you do "1 OR 2" that evaluates to "TRUE OR TRUE" = TRUE which is not what you are looking for.

While in English that might sound sensible, no programming language allows that shortcut with booleans. Some languages allow a switch command to test for multiple IF cases, but it's not available in ZXB.

You need to test (var=1) OR (var=2) to have the right true false values either side of the OR. You might be able to have something like "IF INT ver >0 AND INT var < 5 THEN" if the numbers are in a range, though.


Thanks for the clarification. I was looking for a more efficient way to check several non-consecutive values, but if there is no other way, I'll have to use the 'old' way.

Cheers
Reply
#4
In the future there will be constructs for these. Actually ZX Basic will allow dynamic arrays, and you can use a function like "BelongsTo" or "IsIn" (variable, array) to check for it.
But not yet.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)