Forum
Testing a variable within a range - 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: Testing a variable within a range (/showthread.php?tid=1835)



Testing a variable within a range - worcestersource - 01-27-2022

Hello!

I was wondering if there was a better way of testing whether a variable was within a range.

So instead of:

Code:
IF a > 0 and a < 30 then...

There was a neater and/or more memory efficient method? These sorts of tricks that can help us eke out every last drop out of zxbasic. Smile

Cheers,


Steve


RE: Testing a variable within a range - boriel - 01-28-2022

No, there's not an alternative way. That said, short circuit evaluation is also on the way. In the mean time, if you want more speed, you can do:
Code:
IF a > 0 THEN
    IF a < 30 THEN
      ...
    END IF
END IF

Despite being more verbose, it's more efficient (speed).


RE: Testing a variable within a range - worcestersource - 02-03-2022

Thanks Boriel.

I don't mind verbose. Speed's not a huge factor in my game since it's turn based but keeping memory to a minimum so I can cram everything I want is important. It's amazing the differences using different methods produce.

Steve


RE: Testing a variable within a range - worcestersource - 02-06-2022

Actually, I've been doing some refactoring and gave the IF...THEN IF...THEN a whirl instead of using ANDs and they are about 10 bytes cheaper.

So instead of doing this:

Code:
if eventA = 0 and eventB = 0 then


Do this instead:

Code:
if eventA = 0 then if eventB = 0 then

If have a lot of ANDs in my code! I'm sure I could save quite a lot of memory here!  Smile

Steve

P.S. Would it be an idea to set up a tips and tricks bit of your forum? Such things could help us write faster, smaller code, even if it were not the most logical to read back.


RE: Testing a variable within a range - RandomiserUsr - 03-12-2022

(02-06-2022, 10:37 PM)worcestersource Wrote: Actually, I've been doing some refactoring and gave the IF...THEN IF...THEN a whirl instead of using ANDs and they are about 10 bytes cheaper.

So instead of doing this:

Code:
if eventA = 0 and eventB = 0 then


Do this instead:

Code:
if eventA = 0 then if eventB = 0 then

If have a lot of ANDs in my code! I'm sure I could save quite a lot of memory here!  Smile

Steve

P.S. Would it be an idea to set up a tips and tricks bit of your forum? Such things could help us write faster, smaller code, even if it were not the most logical to read back.

That is actually very very useful to know! Thank you.  In one line only changing to "short circuit"  actually decreased my code by 13 bytes, and every byte saved is great news! Also I would expect it to perform better but I've not been able to test that but I will take it as it does :-)


RE: Testing a variable within a range - RandomiserUsr - 03-12-2022

Further to this I tried it out on an IF/THEN/ELSEIF and found it didn't like it.


e.g.
IF X=1 THEN
    X=2
ELSEIF X=2 AND T< 10 THEN
   X=3
END IF
replaced with this:

IF X=1 THEN
    X=2
ELSEIF X=2 THEN IF T< 10 THEN
   X=3
END IF

I guess it is because it is inside an ELSEIF  and gets confused as to what should be done ?


RE: Testing a variable within a range - boriel - 03-13-2022

The first expression is ok, and it works for me:
Code:
X = 2
T = 9
IF X=1 THEN
    X=2
ELSEIF X=2 AND T< 10 THEN
   X=3
END IF

PRINT X
This will print "3" as expected.

What problem are you having in the first case? Can you elaborate further, please?
Also, can you report which compiler version are you using?


RE: Testing a variable within a range - worcestersource - 03-13-2022

(03-12-2022, 11:06 AM)RandomiserUsr Wrote: Further to this I tried it out on an IF/THEN/ELSEIF and found it didn't like it.


e.g.
IF X=1 THEN
    X=2
ELSEIF X=2 AND T< 10 THEN
   X=3
END IF
replaced with this:

IF X=1 THEN
    X=2
ELSEIF X=2 THEN IF T< 10 THEN
   X=3
END IF

I guess it is because it is inside an ELSEIF  and gets confused as to what should be done ?

I had some ELSEIF logic in my code and found that the ELSEIF...THEN... IF variant confused the compiler, so this bit of the code still uses ANDs.

Does ELSEIF have any advantages over lots of IFs to test a state? My game reads in the keyboard by peeking an address and then evaluates it.

Steve


RE: Testing a variable within a range - boriel - 03-14-2022

I repeat: The FIRST variant is OK, compiles OK, and runs Ok.
What do you mean by "confused the compiler"? (i.e. syntax error?)

Also, the second variant is ok, but you're mixing block IF sentences with single-line IF sentences and is confusing. You can write it as:
Code:
IF X = 1 THEN
  X = 2
ELSEIF X = 2 THEN
  IF T < 10 THEN X = 3
END IF

Which looks clearer.

ELSEIF is like SWITCH in other languages like C/C++. Once a condition is fulfilled, the remaining ones are skipped which leads to faster execution.


RE: Testing a variable within a range - worcestersource - 03-14-2022

(03-14-2022, 08:50 AM)boriel Wrote: I repeat: The FIRST variant is OK, compiles OK, and runs Ok.
What do you mean by "confused the compiler"? (i.e. syntax error?)

Also, the second variant is ok, but you're mixing block IF sentences with single-line IF sentences and is confusing. You can write it as:
Code:
IF X = 1 THEN
  X = 2
ELSEIF X = 2 THEN
  IF T < 10 THEN X = 3
END IF

Which looks clearer.

ELSEIF is like SWITCH in other languages like C/C++. Once a condition is fulfilled, the remaining ones are skipped which leads to faster execution.

Hi Boriel,

Sorry, I don’t think it’s a case that the compiler is wrong but mixing multi-line and in-line IFs causes the unexpected results (or was it a failure to compile? I can’t remember now). Maybe it was not enough or too many END IFs? I don’t think you have anything to worry about here. 

Thanks for explaining the advantage of ELSEIF. I’ll look forward to refactoring my code and hopefully get a tangible speed boost!

Steve


RE: Testing a variable within a range - boriel - 03-14-2022

Just for the record Rolleyes (as someone else might find this thread useful):
Code:
IF/ELSEIF .. THEN <something>
does not require END IF, however
Code:
THEN
  <something>
END IF : REM required
THEN at the end of line starts a block which must be closed with END IF.

In the later case you're using
Code:
ELSEIF X = 2 THEN IF T<10 THEN
  X = 3
END IF
And this is OK, but the END IF if closing the IF T < 10 condition (the ELSEIF ... THEN if followed by such IF, not by an End Of Line, hence no END IF is needed to close it).


RE: Testing a variable within a range - RandomiserUsr - 03-25-2022

(03-14-2022, 09:49 AM)boriel Wrote: Just for the record Rolleyes (as someone else might find this thread useful):
Code:
IF/ELSEIF .. THEN <something>
does not require END IF, however
Code:
THEN
  <something>
END IF : REM required
THEN at the end of line starts a block which must be closed with END IF.

In the later case you're using
Code:
ELSEIF X = 2 THEN IF T<10 THEN
  X = 3
END IF
And this is OK, but the END IF if closing the IF T < 10 condition (the ELSEIF ... THEN if followed by such IF, not by an End Of Line, hence no END IF is needed to close it).

Okay- I have revisited the code again and is compiling as normal, I have no idea what happened.

Thanks again Boriel