Shop OBEX P1 Docs P2 Docs Learn Events
conditional gosubs — Parallax Forums

conditional gosubs

hitswarehitsware Posts: 156
edited 2012-01-24 14:48 in BASIC Stamp
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 ???

Comments

  • Mike GMike G Posts: 2,702
    edited 2012-01-23 15:11
    IF <condition> THEN GOTO Label
    

    Or use a SELECT CASE
  • ZootZoot Posts: 2,227
    edited 2012-01-23 15:26
    Actually, your Qbasic version will almost work as-is in Pbasic. The only difference I can see is that ON...GOTO and ON...GOSUB count the first label as "0", the next as "1", so you could do this:
    DO
      ON x - 1 GOSUB Sub_0, Sub_1, Sub2  ' where X = 1, 2 or 3; if x < 1 or > 3 then no subroutines will run
    ' other code here
    LOOP
    
    
    Sub2:
      ' do stuff 
       RETURN
    Sub1:
     ' do stuff
     RETURN
    Sub0:
    ' do stuff
      RETURN
    

    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.
  • hitswarehitsware Posts: 156
    edited 2012-01-23 15:37
    Thanks !
    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
  • ercoerco Posts: 20,256
    edited 2012-01-23 17:05
    Another DOS PBasic user, amazing!

    Yes, that's the price you pay for using PBasic 2.0 instead of the greatly improved 2.5 ...
  • hitswarehitsware Posts: 156
    edited 2012-01-23 20:03
    erco wrote: »
    Another DOS PBasic user, amazing!

    Yes, that's the price you pay for using PBasic 2.0 instead of the greatly improved 2.5 ...
    So how do you do what I want to do in 2.0 ? ............. OR! ..................Get a DOS version of 2.5
  • hitswarehitsware Posts: 156
    edited 2012-01-24 14:48
    ' BRANCH does the trick
    ' 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
Sign In or Register to comment.