how_20to_20avoid_20gotos
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
how_20to_20avoid_20gotos [2018/03/31 13:19] – external edit 127.0.0.1 | how_20to_20avoid_20gotos [2024/01/05 00:22] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 2: | Line 2: | ||
//by Richard Russell, November 2006, revised June 2007 and October 2011//\\ \\ BBC BASIC includes the **GOTO** statement, but GOTOs are generally considered to be **a bad thing**. I won't repeat the arguments for avoiding GOTOs here: they are well explained in the [[http:// | //by Richard Russell, November 2006, revised June 2007 and October 2011//\\ \\ BBC BASIC includes the **GOTO** statement, but GOTOs are generally considered to be **a bad thing**. I won't repeat the arguments for avoiding GOTOs here: they are well explained in the [[http:// | ||
+ | <code bb4w> | ||
FOR i = 1 TO maxi | FOR i = 1 TO maxi | ||
FOR j = 1 TO maxj | FOR j = 1 TO maxj | ||
Line 13: | Line 14: | ||
100 PRINT "Empty element found at: "i,j,k | 100 PRINT "Empty element found at: "i,j,k | ||
+ | </ | ||
What this code does is to scan through all the elements of a 3-dimensional array, looking for the first empty (zero) element. When found it doesn' | What this code does is to scan through all the elements of a 3-dimensional array, looking for the first empty (zero) element. When found it doesn' | ||
+ | <code bb4w> | ||
i = 0 | i = 0 | ||
REPEAT i += 1 | REPEAT i += 1 | ||
Line 29: | Line 32: | ||
PRINT "Empty element not found" | PRINT "Empty element not found" | ||
ENDIF | ENDIF | ||
+ | </ | ||
This certainly avoids the GOTO, but is it as clear? And what about execution speed? A simple measurement demonstrates that this ' | This certainly avoids the GOTO, but is it as clear? And what about execution speed? A simple measurement demonstrates that this ' | ||
+ | <code bb4w> | ||
imt = 0 : jmt = 0 : kmt = 0 | imt = 0 : jmt = 0 : kmt = 0 | ||
FOR i = 1 TO maxi | FOR i = 1 TO maxi | ||
Line 47: | Line 52: | ||
PRINT "Empty element not found" | PRINT "Empty element not found" | ||
ENDIF | ENDIF | ||
+ | </ | ||
This relies on the ability to terminate a FOR...NEXT loop prematurely by setting its control variable to the limit value (all versions of BBC BASIC let you do that). Note that it is now necessary to copy the indices at which the empty element was found, but the execution speed is at least as good as the first version (it may be faster, because GOTO itself is quite slow).\\ \\ Here is a radically different approach:\\ \\ | This relies on the ability to terminate a FOR...NEXT loop prematurely by setting its control variable to the limit value (all versions of BBC BASIC let you do that). Note that it is now necessary to copy the indices at which the empty element was found, but the execution speed is at least as good as the first version (it may be faster, because GOTO itself is quite slow).\\ \\ Here is a radically different approach:\\ \\ | ||
+ | <code bb4w> | ||
IF FNsearch(i, | IF FNsearch(i, | ||
PRINT "Empty element found at: "i,j,k | PRINT "Empty element found at: "i,j,k | ||
Line 64: | Line 71: | ||
NEXT i | NEXT i | ||
=FALSE | =FALSE | ||
+ | </ | ||
This time we've moved most of the code into a user-defined function. Now we can exit from the loops without using a GOTO, simply by returning early from the function.\\ \\ The code is easy to understand, it runs quickly, and by moving the search routine into a function (which can be placed out of harm's way at the end of the program) we also adhere to one of the other principles of modern software practice: [[http:// | This time we've moved most of the code into a user-defined function. Now we can exit from the loops without using a GOTO, simply by returning early from the function.\\ \\ The code is easy to understand, it runs quickly, and by moving the search routine into a function (which can be placed out of harm's way at the end of the program) we also adhere to one of the other principles of modern software practice: [[http:// | ||
+ | <code bb4w> | ||
FOR i = 1 TO maxi | FOR i = 1 TO maxi | ||
FOR j = 1 TO maxj | FOR j = 1 TO maxj | ||
Line 78: | Line 87: | ||
PRINT "Empty element not found" | PRINT "Empty element not found" | ||
ENDIF | ENDIF | ||
+ | </ | ||
I hope you can see that there are several ways of avoiding GOTO. Not all of them necessarily improve a program' | I hope you can see that there are several ways of avoiding GOTO. Not all of them necessarily improve a program' |
how_20to_20avoid_20gotos.1522502364.txt.gz · Last modified: 2024/01/05 00:17 (external edit)