conditional gosubs
hitsware
Posts: 156
In QBasic if one wants to conditionally goto 1 of 3 subs
(where x is a number 1,2,or 3)
ON x GOSUB One,Two,Three
One:dostuff:return
Two:dostuff:return
Three:dostuff:return
Will do one of the subs selected by x (from the order of the subs)
How can I do this in PBasic ???
(where x is a number 1,2,or 3)
ON x GOSUB One,Two,Three
One:dostuff:return
Two:dostuff:return
Three:dostuff:return
Will do one of the subs selected by x (from the order of the subs)
How can I do this in PBasic ???
Comments
Or use a SELECT CASE
ON...GOSUB and ON...GOTO are actually much more compact -- codespace-wise -- than IF/THEN or SELECT/CASE. Additionally, each ON...GOTO or ON...GOSUB only counts once towards the maximum mumber of GOTO/GOSUBS that are allowed in a program, while IF/THEN with multiple GOSUB/GOTO statements would each count towards the total (you can only have 256 GOTO/GOSUB statements in a program.
But I don't think there is a "on" in PBasic ?
Possible caveat ?
I'm using the original DOS PBasic
The "if then goto" works but not "if then gosub"
if I put "if then gosub" I get "expected:label" error
Yes, that's the price you pay for using PBasic 2.0 instead of the greatly improved 2.5 ...
' listen to pin 15
p var word:c var byte(4):m var nib(4)
x var nib:y var nib:r var word
c(0)=36:c(1)=27:c(2)=20:c(3)=20
m(0)=2:m(1)=4:m(2)=3:m(3)=4
play
for x=0 to 3:for y= 0 to 3
random r
p=c(x)*m(y)*11/4
branch r//3,[Bass,Mid,Hi]
Nxt:next:next
goto play
Bass:freqout 15,270,p,3*p/2:goto Nxt
Mid:freqout 15,270,2*p,3*p:goto Nxt
Hi:freqout 15,270,4*p,3*p:goto Nxt