Shop OBEX P1 Docs P2 Docs Learn Events
If-then and Gosub — Parallax Forums

If-then and Gosub

ArchiverArchiver Posts: 46,084
edited 2001-08-15 17:45 in General Discussion
I feel kind of stupid for having to ask this but, why can't I use a gosub
command in an "If, then" statement?



i.e., If x=10 then gosub blahblah

I have a series of "if then" statements in my main code loop and would like
to return to the next instruction. My work around is that I have labels for
every instruction. Gosub would certainly seem to be cleaner.

Thanks for any advise.

Jeff

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2001-08-15 17:45
    This is just how PBasic works. There is an implied GOTO after the THEN. Here
    are a few ideas to work around it:

    1) The obvious

    IF x=10 THEN call_blah
    blah_ret:
    yadda yadda


    call_blah:
    gosub blah
    goto blah_ret


    2) The subtle

    IF x<>10 THEN nocall_blah
    gosub blah
    nocall_blah:


    Another related trick is to use a label called RET in front of some RETURN
    statement in your program (doesn't really matter which RETURN). That way in
    a subroutine you can write:

    asub:
    whatever
    yadda yadda
    IF x=33 THEN RET ' return if X=33
    blah blah

    ret:
    RETURN


    Al Williams
    AWC
    * Easy RS232 prototyping
    http://www.al-williams.com/awce/rs1.htm



    >
    Original Message
    > From: Jeff Mitchell [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=r__5h6YbHx7Q7zsTOkkvFJ72pHa_5-2k_iwRgvbhabbZPLYsSMaP7rsnWTZ2MpSlM0cPeCHo4ySwFHlb]EL-JEFE@P...[/url
    > Sent: Wednesday, August 15, 2001 11:17 AM
    > To: basicstamps@yahoogroups.com
    > Subject: [noparse][[/noparse]basicstamps] If-then and Gosub
    >
    >
    > I feel kind of stupid for having to ask this but, why can't I use a gosub
    > command in an "If, then" statement?
    >
    >
    >
    > i.e., If x=10 then gosub blahblah
    >
    > I have a series of "if then" statements in my main code loop and
    > would like
    > to return to the next instruction. My work around is that I have
    > labels for
    > every instruction. Gosub would certainly seem to be cleaner.
    >
    > Thanks for any advise.
    >
    > Jeff
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the
    > Subject and Body of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    >
Sign In or Register to comment.