PlayBASIC Tutorial: Intro to GOTO & GOSUB statements

Started by kevin, April 08, 2017, 04:15:07 AM

Previous topic - Next topic

kevin

  PlayBASIC Tutorial: Intro to GOTO & GOSUB statements

  Welcome, in this tutorial we take a look at some none conditional control change statements, or otherwise known as Goto and Gosub statements in PlayBASIC.

Commands used in this tutorial. Goto / Gosub / Return /  For / Next / If / EndIf / Mousex() / MouseY() / MouseButton() / Print / Circle / Sync   and possibly a few others.





 Goto / Gosub Video Transcript


Hello and welcome to another play basic tutorial this time we're going to have a
look at a couple of pretty controversial commands in modern programming one is go
to the other one is goes up now these things are found in the manual I said go
up to help and you find them under call commands where are we okay so these are
under control changes Dan yeah subroutines and of course you can have a
look at them there and read the stuff in there but today's little tutorial was
really just about well really just to get on screens to look at how can you
use these things yeah what a go-to does is it makes play basic and pretty much
any form of basically using jump to the the label we place our it so I need to
make to do two things I need to type in the word car to add a space and then our
label name before just type you know my label now if I try and compile that
we'll get an error we're getting an error because we haven't defined we have
been told that where this label is a label is a special marker that's in your
program it has to be left justified and you need a colon after it yeah
what can I create a pretty useless program I'll put a sink at the bottom
and I'll put on white K so the pram doesn't just end immediately we'll try
and run it again f5 I get a dumb-looking play basic
screen now at this point we can't really tell what's going on I need something in the
in between our go-to statement to show people happen to show that we're
actually jumping over something now if we run the code this time you're
expecting to see the hello will you'll be horribly mistaken there you go
no all right let's have a look at what's actually happening it's when period comes down it sees this guy's whose
statement here goes okay you want me to jump from this point in the code to
where this little label yeah and it's kind of like a marker in the code I could put his new lines down here as I
lie all bunch of print statements pull
back up and spray and so let's say let's go what's going on okay so I have a bunch of print statements there yeah in
really old basics you say the line numbers are on the edge of the screen here which is really just there as a
kind of a courtesy really in modern
programming in the gutter over here you know old basics he would actually have
you you would have line numbers for each line of code and you would jump to the
line number specifically so if we wanted to jump to that happens to be line 20
you would put go to 20 this doesn't work anymore you can't do this anymore right
it's telling yourself there's an error and go to the label go-to was not
defined let's angels in here it doesn't know what to do with that literal value it has to be a libel
let's give it a bit of a better name that label will call this label uh what
about we call it skip over three
statements pretty big level to remember but nice and obvious in the code what's
going on place the label they I don't need a colon to call the label I just need that
as part of its identification sorry it's not Phoebe doesn't consider this to be
an integer or some other kind of key word right let's write it again check it
out nothing on screen so we are jumping from this point here when pv hits this
go to his daemon jumps to wherever in the code that this label is now we can
jump down the code or we can jump up the code at this point we're jumping down
which is so jumping from this this line here down down to here we could do
something like this we could move that
junk in the middle of these print statements and we would say well I have one print statement
so we'll say one hello will and then always once he will get skipped over and
there we go yeah go there was a non conditional jump would or explicitly
jumping every time we hit this instruction so any code that's following
the go-to statement isn't going to be executed unless you at some other point
jumped back to it now can you be a place a label here
I have to go to now if I add a jump
statement sorry it goes to stop it here and jump I have to go to the pram would
keep repeating this this service logic here so it would come through the first time skip over we would say one hello
well but it's there waiting for a key I thought once I press ok you'll hit this
go statement then jump up to here it'll print a bunch of this hello world and
then the label itself doesn't actually do anything it's just a marker for where these go-to statements jump into when
PVC it's easy to meet the code just ignores it just runs straight past them so people would see the print statements
here run them in succession skipping over the label heatsink and then we seen
the away for a key hit go to half I press ok and we just keep doing that
forever once this thing starts I won't finish let's try it out that's the first
time I've got one hello world statement
heaps up luckily I can escape you out of that
it's like good is a non conditional branch it's and a jump thing I should
say and the jumping is forever baby has no what no way of knowing you ever want
to come back from this point another kind of branch which is also known as a
subroutine is the ghosts up might leave
that in there and go semester has the
same requirements we have the keyword then we need a label after it won't help
on goes up for example you just put the cursor on it press f1 and go to the that
particular help it's a favorite of bit of stuff for this one here now go
service is similar to go to and we need
to give it a label but it's actually designed for things called subroutines
an N statement but I want the program to
run once after the weight care I want to hit at this point and then finish I'll
call this label my sub and we'll prints so put a label up answer left hand side
will print my first subroutine yeah if I
ran this we actually see the thing with pretty much this flash on and then
disappear in return as you can see we run again the same thing the reason for
is is that PB is coming down doing our print doing the go-to jumping to this
point here me go sup and jump into this this subroutine here but we haven't told
that the where the end of this subroutine is you know subroutines are kind of a way of making well we have
small pieces of cone that are outside of a loop there can't
be replaced by functions and in P subs in in PB really or in bottom program
they're generally so they kind of like a legacy basic approaches like you mix and
match the two yeah some people that like the use of go-to and go so that's fair
enough but you can do it not the end of the world go sub expects at some point after
this label you're going to use what's called the return command this is the
pairing so kosub statements have to be paired with a return statement at some point the mechanics of how go sub works
I'll go through that in a second but this time we're calling this subroutine
which means we're jumping down to here it's labeled running whatever code is is
after that hitting the return statement and yeah that's it this return actually
this time jumps us back to a line after the goes up so we can break away from
this mean main flow of code do something and then return back to the main flow
continue on this time I will see there we go now print same at the top and
without for subroutine working here as well let's put that into a four next
live or something like that so you can see this thing is we're calling and returning so a loop the thing I though
that many times ten times if you don't update with for next loops gonna have
all the other tutorials on for next loops get an idea what's happening this
time but I've got a loop yeah every time in we process through the loop we're
going to be calling the subroutine doing this thing here which is the print
statement in returning so it's going to print that message 11 times
yep I said 11 times 1 2 3 4 5 6 7 8 9 10
11 uh-huh look at that we have an era why
is that giving us an arrow what must be some bug and fly bicycle well it's not actually we have to kind of go back a
bit to understand how it goes up works that understand what this error here it's telling us there's a stack under
flow that's because we haven't got any statement in here yeah before I was
talking about when we have labels when PP sees a label in the code it doesn't
care about it it doesn't think that's that's not code it's just a markup
within the program so people would you know I'd say the label it just skips
straight straight past it it remembers where that location is within the code but it doesn't doesn't generate any
actual code itself the label so let's go
back and go over the mechanics of gosub previously I talked about that goes up
statement when it's executed
jumps to the location with it with a label actually is within the program now
it does something else - is it actually adds what's called a breadcrumb really adds the I guess for
simplicity sake we'll say and the position of the next line of code after
this gosub statement on to what's called the stack the stack is a it's just a
heap of a piece of memory that's used like a heap things are thrown onto the
stack generally in a temporary fashion then retrieved from the stack and the
idea is to have a stack always at 0 if we try and retrieve information of
the stack that's empty we get an underflow that's what's happening here on that previous row anyway so so when I
go sub statement is call it adds to the stack the position in the code after the
go sub statement that's important to note and then it jumps till this this
label here so we go okay looks at the weenus up well the next
line is in in the code push it to the stack and then jumps to the go sub
labels location which is my sub in this case continues on executing as normal
so the code here is got a print statement we run the print statement then we keep this return Simon is the
the statement that is paired with kosub statements it's the mechanism that that
retrieves an old retrieves the location
of what we have to jump back to from the
stack so when we jump we jump to my sub on the stack we've got one value that's
going to be this location here when we
get down to here we're going to pop this value we just pop just means to grab the
top value on the stack since only has one value we're going to grab that value
and we're going to use that as the return location now what they jump back
to location so return is kind of like a like a go-to statement actually that is
using the stack mechanism to retrieve where it has to go to back or has to
jump back to wow that's pretty clumsy clumsy explanation I think hopefully
that sort of makes a bit of sense so if we did something like this here
we're calling it the ghost up once within the routine we call it these four
times you can work out how about how much stack data is required it's never
needs more than one bin stack stack value because this guy's your statement
is going to push data to the stack so I've got one value on the stack it's gonna get a run this bit of code here and then returns it emptying the stack
out to zero okay but if we had another
ghost up in here let's say we print this we go Mike
well let's let say we jump to a second a second routine called my second sir
then we'll print something out here as well my second so everything create huh
we need that return Satan on the M they're very important to have that this
time we actually do have no more valleys on the stack so forget back at the top
we entering out our loop we have a call to my sub it's pushing the return value
to the stack at this point and jumping to my sub start executing this all good
it's another go sub statement the stack already has one value on it's the old
that's the return return value or from my sub this original call here well yeah
we want it goes up somewhere else which just happens to be the next line below it you wouldn't do that in real life but
hey that's an example so wait this goes up here is pushing the next line which
is this return statement anyone to stack so we have two bits of
data on the stack and then jumping down to here my second stub so we're doing
this print all cool and hitting return
so we've retreat with pop the stack once so that last value pushed on with the
this value here so we return back to line 17 in this case runs this code here
hits the return and that return will bring us back to line eight up here emptying out the stack pretty crazy
oh there we go you know I suspect people would think that's a bug there's
actually not it's intentional and play basic something that the label wasn't defined that's because PV has very
strict rules about how labels can be used it's kind of one of those things
that's a minute of kind of upset people one of reasons why people don't like go
to it in PB labels have to be right up against this left edge if it's not a few
picked if you try to indent it it won't see it as a label at all no it's when it
hit this bit of cardio and won't have a clue that my second son it is defined
that's intentional it means that you you then can't go putting labels and strange
places off push it out do a bit on that when we do talk about functions anyway
right there we have we've got my first sub my second serve my first my second month so one all the way down okay if
you're wondering what we probably we're not sure that I got like having why we
had a stack on the floor earlier our mood is in statement people look at this
this loop here well get rid of the lip altogether just have like bad bones so
pulling out that subroutine so we're pushing some dye into the stack doing
this subroutine yeah doing the second subroutine jumping tonight doing its work here returning back to here isn't
returning back to the original call doing its sting come in waiting when I press a key what's gonna happen is
baby's gonna move past down fall down to here call this guy's I'm like run this
subroutine here return and then hit return statement yeah the stack at this point won't have any
data right you'll only have could be empty so you actually have a
stack under flour just to verify that's the case so if you're running a program
you're getting all these errors at means at some point you can use to go so that's pretty much what it wants to
what's telling you that's always in the end statement for you so after it after
I hit a key which I'm the prime to just finish we're done quit okay so there's a
couple of other types of statements that we could use and these are probably a little bit more advanced these are the
on goes on on go to variations yeah
probably one of the reasons why people that like like go-to in general or even
subroutines today so we have other other mechanisms now that emulate them such as
the functions s functions or P subs which is a protected subroutine we're
just kind of a mechanism that kind of emulates what this does but it looks like it's a much more modern looking
solution so we can we can we can branch
away from this main piece of code and do
something and then return so this time
we're we're calling my sub which has just a print statement and a return but
when we get back from this first call we're gonna call here to get press the
same output as before
okay
let's have a look at we might use a
go-to statement hopefully you've looked at it will sort of lips a little beaten
confidently with ice things or you know
kind of make an example where it's probably not what sorry I'm gonna show a
comparison rather than actually have our main do loop here prior round I'll just
make the inner loop here do something draw a circle like a mouse coordinate
thirty and true alright so we run this thing here
join my blog where the mouse position is your skype and we're out yeah you know
all the basics didn't have two loops probably some variations of them to what some people might like to do actually
it's people who come from those languages they like to actually have
[Music] this construction so you have like a main loop up here and you have guys who
may live at the bottom this replicates
the same behavior a program starts executing does a clear screen draws a
circle over the mouse position flips the back up into the front buffer hits the Kosar jumps up here just keeps doing
that forever
you know cuz I've got a sink in a way that's actually how when that was the boring we can turn that behavior
behavior off it really wants a wave sort of learn at winning it's very useful we
couldn't use guys up here and the reason police is all we should actually get a
stack overflow if we do this or try and see what happens
it's running for a bit
well yeah it's giving us a bit of an error
there we're running on till about like a beta version of t bay in these videos
and the pages don't have all the same ear attracting that the general runtimes
do but what an overflowing is is that the stack has a has a limited space in memory at some point if you keep pushing
stuff to it it just overflows and PB normally can detect those things and
stop it from from crashing like that
okay what I'm gonna do is I was gonna do let's say okay let's say you want to
press the mouse once ap click the mouse
button and you want this thing to end the program you couldn't use it go to
here you can go to as long as your label is outside of this loop and below it
and my primal corner label so it's like it the label is just like a position a
marker in the Unicode so when I press
the mouse button the program is running this condition is going to be true if
it's gonna stay if something's gonna fall through hit this ghost go to there it's going to change controls gonna jump
to wherever this in label here it's which is down here since that's at the end of the friary the thing's gonna end
let's try it already click the button and there we go
yeah I mean some people don't like those those things but
which is the why the why it is one other other mechanism we do have is probably a
little bit more advanced I guess it gives us well as the on go to or on
variable going to which is kind of another way of doing so I find my
variable they're going to and then I want a new list of labels at the end of
it - and that's right explain this again in
a second don't we have any preconceived ideas all its gonna do is gonna write something
and then try and get it to work for you
put a sink at the bottom I know one just thing to stop
let's think it's foolish stuff inside a for next loop just because hey we can
we'll make this loop go from zero to three so what on goes here does is it
uses this variable here as the offset into a table of labels as long as the
long as you've got in this case loop is going to be a value between north three so as long as it's within range it's all
going to be good and well so in other words what's going to happen is that
when loop is zero the first time through it's going to go to label zero which is
here just print print zero now what
we're saying before about here they just ignoring labels you might notice that
we'll just hit the label and discuss straight past it so when it's if Luke is
zero it will actually print all of these values you might just play the phonics
loops they can see that for sure is your own Oh close it run the thing and it's
displaying all of them I change loop to be too we should get
two and three there we go that's because it's when Lucas - that's index 0 that's
index one that's index 2 and the table that's so go - is going is jump in to
label do this one the printer has no other control changes
after it so just falls past and prints three if we had another guy so in the
jump table which is really what we're
making here by the way I'm making a jump table you can't control the little the
core mechanics of how this thing works this time we won't get the extra values
so we'll just just wait so for us there we go if we put four loop into into
those
all right into our next statements this thing's getting pretty long so that we
want to keep bashing around the head with this let's run again let me get all
right so first time through the loop loop is zero so jumps up jumps - look
this this part in the table jump sorry jumps on this label here when we whip
again Luke is gonna be one it uses label 1 which is down here so you print one on
the second time through if I create loop here it's pretty easy to see that there
we go sewing labels 3 we jump into label
3 down the bottom it's a kind of simulates a I guess a select case
statement in sort of modern programming speak which some old boxers didn't have but some of them did ok hopefully that
gives you some ideas about what these things you know or even some reminders
about what's actually you can do with these kind of really basic ideas you can use that there's a variation in this ah
there's a go stop statement - by the way to make this work as it goes up you
would need to put put a jump here and returns after these statements here
these wouldn't even need to be to be in here actually it makes more sense for them not to be in there
[Music] does that work to incitement they return
here get rid of the Heysel no Missy is
that pretty messy don't need this stuff here at all that's different in there
otherwise I've got stack on the floor we run that same behavior and we're happy
so you can use that use the same mechanic to call a list of subroutines on whatever whatever some variable is
I've use that in the past to do things like like in there's a sprite collisions
that kind of thing where you might have different character numbers and that character number has just been in a list
of those things other people use them in select case statements the principle is
pretty much the same well I think that will have to do us with a calendar some
pretty boring stuff today I'm sorry about that it's a hope that just gives
you so many or what these kind of statements do and you my bumbling wasn't
to do you hard to follow anyway thanks
for watching and hopefully probably start to progress and just keep it up
you





ON VARIABLE GOSUB

PlayBASIC Code: [Select]
      for lp =0 to 3
print lp
on LP gosub Zero,One,Two,Three
next

sync
waitkey
end


Zero:
print "Zero"
return

One:
print "One"
return

Two:
print "Two"
return

Three:
print "Three"
return







stevmjon

i liked learning about the stack. those details are interesting.

the breakdown of the gosub and goto was good. i understand better about exiting a loop using goto, but is see it is better to use the exit command.

keep the videos rolling, i like getting these kinds of details in all the videos so far. i am glad you found a good way to fill in the boring sundays.

  stevmjon
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

kevin

 Steve,

Quotei understand better about exiting a loop using goto, but is see it is better to use the exit command.

   they actually produce the same code, as no difference in how they work internally..   they both resolve to absolute JMP's in the runtime.    A Goto just lets you pin point the location where the jump lands..   


Quotei like getting these kinds of details in all the videos so far. i am glad you found a good way to fill in the boring sundays.

   yeah..  it's the little things that people often miss, some are key points, while others are no doubt less important..  This one recorded it mid last week,  took ages to age titles..