Loops alternate recursive ways
#1
For Dimster request, sorry couldn't find what I was recalling so we can go with this simple demo:

The sum of intergers 1 to 100, 3 ways:
Code: (Select All)
_Title "Recursive replacement for Loop" ' b+ 2023-01-28

For i = 1 To 100 ' normal loop
    tot = tot + i
Next
Print tot

i = 1: fini = 100 ' GoSub method
GoSub counting
Print totGOSUB

Dim Shared sum ' global sum for saving subtotals in for recCount Function
Print recCount(1, 100)
End

counting: ' recursive because it calls until i hits 100
totGOSUB = totGOSUB + i
If i < 100 Then i = i + 1: GoSub counting
Return

Function recCount (i, fini) ' recursive because it calls itself until i hits 100
    sum = sum + i
    If i < fini Then recCount = recCount(i + 1, fini) Else recCount = sum
End Function

EDIT: more comments
b = b + ...
Reply


Messages In This Thread
Loops alternate recursive ways - by bplus - 01-28-2023, 07:45 PM
RE: Loops alternate recursive ways - by Dimster - 01-28-2023, 09:09 PM
RE: Loops alternate recursive ways - by bplus - 01-28-2023, 09:28 PM
RE: Loops alternate recursive ways - by Dimster - 01-29-2023, 04:10 PM
RE: Loops alternate recursive ways - by bplus - 01-29-2023, 04:23 PM
RE: Loops alternate recursive ways - by Dimster - 01-29-2023, 04:33 PM
RE: Loops alternate recursive ways - by bplus - 01-29-2023, 05:45 PM
RE: Loops alternate recursive ways - by Dimster - 01-30-2023, 05:36 PM
RE: Loops alternate recursive ways - by bplus - 01-30-2023, 07:11 PM
RE: Loops alternate recursive ways - by SMcNeill - 02-01-2023, 06:10 AM
RE: Loops alternate recursive ways - by Dimster - 02-01-2023, 02:05 PM



Users browsing this thread: 5 Guest(s)