PDA

View Full Version : Alternative to the GOTO command for a TB newbie?



MattW
17-01-2013, 13:00
Hi,

I'm still in the transitional period between QBASIC and TB, so please bear with me here.

I always research things like this before posting, I've found some threads regarding the GOTO command but I did not understand the conclusion of how to structure this instead of using GOTO and GOSUB


FOR a=1 to 20
GOSUB displayinfo
NEXT a
' maybe the gosub command is needed elsewhere...
FOR a=1 to 50
GOSUB displayinfo
NEXT a
STOP

displayinfo
PRINT "The counter is currently at ";a
RETURN



Also, I used to GOTO command almost everywhere in QBASIC, like...


IF a<>0 THEN GOTO jumpover1:

PRINT "ahhh! A is not Zero!!"
GOSUB dosomethingwhenaisnotzero

jumpover1:

PRINT "So now I will continue..."


---------
Could I please ask for someone to show me an alternative to the above?
From what I've read, I would adapt my coding to use the FUNCTION commands. These were in QBASIC also, but I think I only used it once (about 20 years ago!!) and I do not know how this will 'replace' my GOTO method.

Many thanks,

Matt

ErosOlmi
17-01-2013, 15:05
First example



Uses "console"
Long a
For a=1 To 20
displayinfo
Next
' maybe the gosub command is needed elsewhere...
For a=1 To 50
displayinfo
Next a
WaitKey

Sub displayinfo()
PrintL "The counter is currently at ", a
End Sub



Second example


Uses "console"
Long a

If a<>0 Then
PrintL "ahhh! A is not Zero!!"
Else
PrintL "So now I will continue..."
End If
WaitKey

Michael Clease
17-01-2013, 15:15
FOR a=1 to 20
displayinfo() 'GOSUB displayinfo
NEXT a
' maybe the gosub command is needed elsewhere...
FOR a=1 to 50
displayinfo() 'GOSUB displayinfo
NEXT a
STOP


SUB displayinfo()
PRINT "The counter is currently at ";a
END SUB




IF a<>0 THEN
PRINT "ahhh! A is not Zero!!"
dosomethingwhenaisnotzero("Scratch my bum")
else
PRINT "So now I will continue...because a is zero"
END IF
STOP


SUB dosomethingwhenaisnotzero(sText as string)
msgbox 0, sText
END SUB