Posts: 18
Threads: 4
Joined: Dec 2020
Reputation:
3
01-27-2022, 08:14 PM
(This post was last modified: 01-27-2022, 08:20 PM by worcestersource.)
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.
Cheers,
Steve
Posts: 1,790
Threads: 56
Joined: Aug 2019
Reputation:
25
01-28-2022, 11:19 AM
(This post was last modified: 01-28-2022, 11:19 AM by boriel.)
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).
Posts: 18
Threads: 4
Joined: Dec 2020
Reputation:
3
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
Posts: 18
Threads: 4
Joined: Dec 2020
Reputation:
3
02-06-2022, 10:37 PM
(This post was last modified: 02-06-2022, 10:38 PM by worcestersource.
Edit Reason: Clarity of keywords
)
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!
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.
Posts: 65
Threads: 19
Joined: Feb 2021
Reputation:
0
(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!
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 :-)
Posts: 65
Threads: 19
Joined: Feb 2021
Reputation:
0
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 ?
Posts: 1,790
Threads: 56
Joined: Aug 2019
Reputation:
25
03-13-2022, 11:38 AM
(This post was last modified: 03-13-2022, 11:38 AM by boriel.)
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?
Posts: 18
Threads: 4
Joined: Dec 2020
Reputation:
3
03-13-2022, 05:45 PM
(This post was last modified: 03-13-2022, 05:49 PM by worcestersource.)
(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
Posts: 1,790
Threads: 56
Joined: Aug 2019
Reputation:
25
03-14-2022, 08:50 AM
(This post was last modified: 03-14-2022, 08:51 AM by boriel.)
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.
Posts: 18
Threads: 4
Joined: Dec 2020
Reputation:
3
(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
Posts: 1,790
Threads: 56
Joined: Aug 2019
Reputation:
25
03-14-2022, 09:49 AM
(This post was last modified: 03-25-2022, 11:50 PM by boriel.)
Just for the record (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).
Posts: 65
Threads: 19
Joined: Feb 2021
Reputation:
0
03-25-2022, 09:59 PM
(This post was last modified: 03-25-2022, 11:50 PM by boriel.)
(03-14-2022, 09:49 AM)boriel Wrote: Just for the record (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
|