Why does my Loop end after 11 Loops?
#32
Hi Dimster
Hi fantastic QB64 coders

I come back with a little demo on recursion...
it lets you play with 3 way to use variable in the main and in the SUB recursive...
the first is that suggested by Dimster and it does not work
but  it needs of STATIC variable or STATIC SUB/FUNCTION to work properly...(the forth)
the second is that uses flag as parameter of SUB
the third is that uses flag as global variable (DIM SHARED)

Code: (Select All)
Rem Demonstration of variables into recursive calling
Screen 0
Dim counter As Single
Dim Shared counter2 As Single
Dim Choice As String

Choice = " "
Do
    If Choice <> "" Then
        Cls
        Print "we are testing recursive calling"
        Print String$(60, "#")
        Print "please make your choice: "
        Print " press 1 for recursion without parameter or shared variable"
        Print " press 2 for recursion with parameter and no shared variable"
        Print " press 3 for recursion with shared variable and no parameter"
        Print " press 4 for STATIC recursion without parameter or shared variable"
        Print " press 0 to exit from demonstration"
        Print String$(60, "#")
    End If
    Choice = InKey$
    If Choice = "0" GoTo Ending
    If Choice = "1" Then GoSub NoParameters
    If Choice = "2" Then GoSub YesParameters
    If Choice = "3" Then GoSub SharedVariable
    If Choice = "4" Then GoSub StaticNoParameters
Loop
End

NoParameters:
counter = 0
Print " No parameter and no shared variable demo"
Print "-----------------------------------------"
Print counter; " value of flag in the main"
RecursiveNoParameters
Return

YesParameters:
counter = 0
Print " Yes parameter and no shared variable demo"
Print "------------------------------------------"
Print counter; " value of flag in the main"
RecursiveYesParameters counter
Return

SharedVariable:
counter2 = 0
Print " No parameter and Yes shared variable demo"
Print "------------------------------------------"
Print counter2; " value of flag in the main"
SharedVariables
Return

StaticNoParameters:
counter = 0
Print " STATIC and no parameter and no shared variable demo"
Print "-----------------------------------------"
Print counter; " value of flag in the main"
StaticNoParameter
Return

Ending:
Rem here the flow of code ends
End


Sub RecursiveNoParameters
    counter = counter + 1
    DoJob counter
    If InKey$ <> "" Then Exit Sub ' emergency exit
    If counter < 10 Then RecursiveNoParameters
End Sub

Sub RecursiveYesParameters (c As Single)
    c = c + 1
    DoJob c
    If InKey$ <> "" Then Exit Sub ' emergency exit
    If c < 10 Then RecursiveYesParameters c
End Sub

Sub SharedVariables
    counter2 = counter2 + 1
    DoJob counter2
    If InKey$ <> "" Then Exit Sub ' emergency exit
    If counter2 < 10 Then SharedVariables
End Sub

Sub StaticNoParameter
    Static counter ' you need to have STATIC only the flag of recursion, at least
    counter = counter + 1
    DoJob counter
    If InKey$ <> "" Then Exit Sub ' emergency exit
    If counter < 10 Then StaticNoParameter
End Sub


Sub DoJob (c As Single)
    Print c; " press a key to stop the recursive loop"
    Sleep 1 ' we need this to avoid the crash of application
End Sub

Just to mantain a modern style I use GOTO and GOSUB for I/O handling...
Reply


Messages In This Thread
Why does my Loop end after 11 Loops? - by Dimster - 02-06-2023, 07:08 PM
RE: Why does my Loop end after 11 Loops? - by TempodiBasic - 02-07-2023, 11:42 PM



Users browsing this thread: 25 Guest(s)