Replacement for GOTO command
Posted: Mon Oct 27, 2003 1:14 am
Code updated For 5.20+
This is continued from my previous topic in the Beginner's forum. I'm sure that you people here know this already, but it sure seemed like a great trick to me.
Thanks!
I made that change and then noticed that I'd created a never-ending loop, as I never again declared do_over to be equal to 0 again. I rewrote the code, but I was having trouble breaking off the number matching early. If, for example, J was 93, and the random number matched the first number in the array, I didn't want the whole array checked. I wanted the check broken off and a new random number created, and the whole process started again.
Without a GOTO command, this seemed too difficult and I at first wrote the following:
But then I realized that a Return command from a subroutine could be nestled within an If/Then statement, and that this could provide the same functionality as a GOTO statement. So I wrote the following. It includes comments, and is the version that I'll post on my website (as soon as the blackout is over and the DNS Engineering, Inc. (my host) servers are back up.
This is continued from my previous topic in the Beginner's forum. I'm sure that you people here know this already, but it sure seemed like a great trick to me.
Thanks!

Without a GOTO command, this seemed too difficult and I at first wrote the following:
Code: Select all
Dim long_array.b(94)
For J = 0 To 93
While long_array(J) = 0
random_number=Random(93) + 1
do_over = 0
For K=0 To J
If random_number = long_array(K)
do_over = 1
EndIf
Next K
If do_over = 0
long_array(J) = random_number
EndIf
Wend
Next J
Code: Select all
;Arrays may not be declared on the fly, but must be declared before they are called.
;Because I know that I will only be useing 94 numbers, I've used a .b, or bit array.
;The ".b" tells the program what type of array it will be -- I won't need to type it again.
Dim long_array.b(94)
;This is the part that creates a random number
;It calls a function that is found at the end of the program.
;Note my use of Repeat commands nestled in the function as a
;replacement for a GOTO command.
For J = 0 To 93
Repeat
do_over = 1
random_number=Random(93) + 1
Gosub random_match
Until do_over = 0
long_array (J) = random_number
Next J
;This is used to view the array
;It displays 25 numbers on each line.
OpenConsole()
For K=0 To 24
Print(Str(long_array(K)))
Print(" ")
Next K
PrintN("")
For K=25 To 49
Print(Str(long_array(K)))
Print(" ")
Next K
PrintN("")
For K=50 To 74
Print(Str(long_array(K)))
Print(" ")
Next K
PrintN("")
For K=74 To 93
Print(Str(long_array(K)))
Print(" ")
Next K
;My standard "end of program" text
PrintN("")
Print("Press 'Enter' to continue")
pause$=Input()
CloseConsole()
End
;random number matching function
;This is where I got my "GOTO" equivalent.
;If the random number matches a number used already, the
;function terminates early, without declaring ;do_over = 1
;Thus. the Repeat/Until loop continues to create a new
;random number and to call this function again until a unique
;number is found.
random_match:
For K=0 To J
If random_number = long_array(K)
Return
EndIf
Next K
do_over = 0
Return