Forum
Evaluate if one variable has several values - Printable Version

+- Forum (https://www.boriel.com/forum)
+-- Forum: Compilers and Computer Languages (https://www.boriel.com/forum/forumdisplay.php?fid=12)
+--- Forum: ZX Basic Compiler (https://www.boriel.com/forum/forumdisplay.php?fid=11)
+---- Forum: Help & Support (https://www.boriel.com/forum/forumdisplay.php?fid=16)
+---- Thread: Evaluate if one variable has several values (/showthread.php?tid=859)



Evaluate if one variable has several values - oblo - 11-08-2018

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


Re: Evaluate if one variable has several values - britlion - 11-12-2018

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.


Re: Evaluate if one variable has several values - oblo - 11-12-2018

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


Re: Evaluate if one variable has several values - boriel - 11-12-2018

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.