So what is wrong with a "Goto" loop and what is so good about a "recursive" procedure call?
What is the best loop to use? Where and why? Maybe this will help.
This program illustrates three types of loops. The first is a standard repeat/until loop. The second uses a recursive call, and the third uses Goto lablel.
I see no problem using a "Goto label" approach as long as the goto "call" and the goto "label" DO NOT cross a procedure block boundary, HOWEVER, the recursive call seems to insure problems. Bring up your Windows "Task Manager" and then run the program.
HINT: The key to this demonstration is to restrict your typing to the visible client area of the StringGadget WITHOUT scrolling the text. This theoretically means the same buffer space will be re-used each time you re-type a complete line. This is where the second loop fails and memory use increases with each line you type.
Code: Select all
If OpenWindow(0, 0, 14, 460, 100, "", #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered )
TextGadget(0, 60, 5, 340, 50, "")
StringGadget(1, 60, 60, 340, 20, "")
EndIf
Event.w=0
;__________________________________________________
SetWindowTitle(0,"Loop #1 using a standard ''Repeat'' loop")
SetGadgetText(0,"Open your ''Task Manager'' and monitor memory use as you type a complete line. Memory goes up, but then remains stable each time you ''delete the line'' and type a new line. Don't type until you scroll.")
SetGadgetText(1,"Press Alt + F4 to enter next loop...")
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
;__________________________________________________
SetWindowTitle(0,"Loop #2 using a ''recursive'' procedure call")
SetGadgetText(0,"Now into the ''recursive'' loop, the memory use goes up each time you type a line and delete it. You may have to try several times, and remeber not to type until the text starts scrolling.")
SetGadgetText(1,"First Loop Complete. Press Alt + F4 to enter next loop...")
Procedure RecursiveCall()
Event = WaitWindowEvent()
If Event = #PB_Event_CloseWindow
ProcedureReturn
EndIf
RecursiveCall()
EndProcedure
RecursiveCall()
;__________________________________________________
SetWindowTitle(0,"Loop #3 using ''Goto''")
SetGadgetText(0,"Now into the ''Goto'' loop, the memory use becomes stable again no matter how many times you re-type the line.")
SetGadgetText(1,"Second Loop Complete. Press Alt + F4 to enter next loop...")
GotoLooP:
Event = WaitWindowEvent()
If Event <> #PB_Event_CloseWindow
Goto GotoLooP
EndIf