Weekly Qbasic and Qb64 Lesson Topics
March 28, 2024, 10:35:27 am
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Want to see a specific Tutorial? ASK!
 
  Home Help Search Arcade Links Staff List Login Register  

Goto's and loops dont mix will if done improperly

Pages: [1]
  Print  
Author Topic: Goto's and loops dont mix will if done improperly  (Read 821 times)
Dustie Bear
Full Member
***
Posts: 115


« on: March 28, 2011, 10:33:23 am »

Hi all

Before we ruffle our feathers, its ok to use GOTO most of the time but
not all the time.

 There are times when GOTO should NEVER be used.
I will explain so bear with me through this.

Use goto when appropriate if you like using them
but NEVER use them to exit a DO loop or FOR loop.
Nor any thing else that resembles a form of a loop

 The stack is a list of memory addresses which is constantly
changing. later on here I think you will see what I mean. so keep reading.

The following is not a loop to which I am speaking because
the address where 10 , 15 and 20 reside is not saved in whats called a
stack.

its ok to do this: etc

Quote
10

15 do stuff

20 goto 10


But when using a loop dont do this

Quote
Do

a = a + 5
if a = 25 then Goto there
loop until x$ = inkey$

there:

And dont do this:

Quote
for t = 1 to 30
if t = 12 then goto There
next t

There
:

to exit a loop of any kind then use the EXIT statement. That
will clear the stack.

The reason is that the program uses whats called a stack
which is a list of pointers to where in memory your loop is
running at the moment. in other words when you enter a loop the
beginning and ending address of that loop are recorded in the stack.
that is how the program know what part of the program to loop within.
it says run from such and such address and loop back tosuch and such
address. These are all addresses in memory which can change constantly
depending what your program does.

So if you exit that loop with a GOTO, that information stays in the stack,
if you do that long enough the stack will eventualy fill up and crash your
program or do something unexpectedly because the GOTO statment does not clear the stack of that info. The stack will eventualy fill up and crash your program if done enough times.

The EXIT statement will clear the stack properly

These are examples of leaving loops properly.

examples:

Quote
Do
x = x + 1
if x = 10 then EXIT DO
loop

Another example to use exit

Quote
for t = 1 to 30
if t = 12 then exit for
next t



and it is ok to call a sub routine from within a loop because that sub will
return back to the loop where it started, thus not messing up the stack.


It is also ok to use a GOTO within a loop that doesnt leave the loop,,, This is a very dumb example but gets the point across I think.

Quote
DO
 
Again:
a = a + 1
if a < 5 then goto again

print "We counted to 5 but nobody seen it";
EXIT DO

LOOP
sleep
system

I hope Ive explained this fairly well.  Im not against using goto sometimes but
there are situations where it is not good practice.

Dustie






« Last Edit: March 28, 2011, 12:24:44 pm by Dustie Bear » Report Spam   Logged

Share on Facebook Share on Twitter

GarrisonRicketson
Admin
Administrator
Hero Member
*****
Posts: 583



WWW
« Reply #1 on: March 28, 2011, 09:54:35 pm »

I would need to go over this a few times, to get it all, but this:
Quote
So if you exit that loop with a GOTO, that information stays in the stack,
if you do that long enough the stack will eventualy fill up and crash your
program or do something unexpectedly because the GOTO statment does not clear the stack of that info. The stack will eventualy fill up and crash your program if done enough times.

The EXIT statement will clear the stack properly
brings to mind, and probably explains, why some previous (firsts), experiments, I can't really call them programs, as that was the problem, they worked, at first, but later start messing up each time I try to run it again,...
 Thanks for bringing this up Dusty, that is a important aspect, rather the saying never use GOTO, learn, or explain, when not to, and when it is appropriate,...
thanks from Garry
Report Spam   Logged


Pages: [1]
  Print  
 
Jump to:  

Powered by EzPortal
Bookmark this site! | Upgrade This Forum
SMF For Free - Create your own Forum


Powered by SMF | SMF © 2016, Simple Machines
Privacy Policy