Why does my Loop end after 11 Loops? - Printable Version +- QB64 Phoenix Edition (https://staging.qb64phoenix.com) +-- Forum: QB64 Rising (https://staging.qb64phoenix.com/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://staging.qb64phoenix.com/forumdisplay.php?fid=3) +---- Forum: Help Me! (https://staging.qb64phoenix.com/forumdisplay.php?fid=10) +---- Thread: Why does my Loop end after 11 Loops? (/showthread.php?tid=1448) |
RE: Why does my Loop end after 11 Loops? - Dimster - 02-11-2023 So the IF ... ELSE is mandatory in the C computer language? "or is something just called after a counter"... The CALLing of the Recursion is naked call within the Recursive Loop ie pseudocode Recur Sub Recur Count = Count +1 if Count = 4000 then Exit Sub Recur <-- Naked Call, ie no conditions just do it again End Sub RE: Why does my Loop end after 11 Loops? - Kernelpanic - 02-11-2023 I only took the example from C because it was explained well there. It can also be found in the QuickBasic 4 manual under 2.44. I don't know whether your code example really runs recursively, I will have to try it out. To me it looks like it is rather iterative. Looks like a procedure to me too. This is now the factorial iterative. It doesn't need a stack. In the past, in the days of Win 95, you could still see a time difference between iterative and recursive, especially when calculating the Fibonacci number. With normal calculations one can today no longer see the difference. Code: (Select All) 'Fakultaet iterativ mit FOR-Schleife - 11. Feb. 2023 RE: Why does my Loop end after 11 Loops? - Dimster - 02-11-2023 Well you inspired me to put my Basic books and Binders of notes aside and check it out on the internet. You are correct. While I could not find Recursion using Basic there is a lot with all the other computer languages. Recursion is much more complex than I thought. I'm using it as a substitute for a Do...Loop or For...Next which I think would be classified as Direct Recursion. Shame there is not a lot on the internet about the Basic language and teaching examples of Recursion in Basic and all various ways to use it. RE: Why does my Loop end after 11 Loops? - mnrvovrfc - 02-11-2023 Recursion is almost a necessity in LISP and in its leading descendants Common LISP and Scheme. RE: Why does my Loop end after 11 Loops? - SMcNeill - 02-12-2023 (02-11-2023, 08:44 PM)Dimster Wrote: Well you inspired me to put my Basic books and Binders of notes aside and check it out on the internet. You are correct. While I could not find Recursion using Basic there is a lot with all the other computer languages. Recursion is much more complex than I thought. I'm using it as a substitute for a Do...Loop or For...Next which I think would be classified as Direct Recursion. Shame there is not a lot on the internet about the Basic language and teaching examples of Recursion in Basic and all various ways to use it. The BASIC concept for recursion is very, very simple: Have a routine that simply calls itself. That's it. That's all recursion is, in a nutshell. If you have a routine which calls itself, then you have recursion. Now, what does a routine need to successfully perform recursion? 3 basic things; 1) A starting point. 2) An end point. 3) To be limited in scope. Recursion which occurs a million times would be best served by some sort of loop structure. Each call to recursion uses a "chunk" of memory the size of your recursive sub/function (after all, it can't exit until it gets back from the recursive call), so if you repeat some process too many times, your program is going to crash when it runs out of memory available. ^But that's basically your only requirements to write a recursive routine. Let me show a few examples for you: Code: (Select All) RecurseSubPrint 1, 20 Now here, I'm passing my variables via parameter. There's a start point, and a finish point, and in this case it only runs 20 levels deep with recursion. All checks pass, so this is a valid recursive routine. Code: (Select All) FUNCTION RecurseFunPrint (finish) Now here, I'm keeping the starting point STATIC (count) and passing my end point back to my recursive routine (finish), and this too is only going to be for 20 levels of recursion in my program. Now note, this WORKS, but **it's only going to work ONCE**. By making my start point STATIC here, there's absolutely no way to reset it. Count will increment by one every time it's called, and it's got no way to reset it. Why someone would want a recursive routine to behave in this manner, I have no clue, but that's what it's going to do here. Start Point. End Point. Limited number of calls back to itself. That's it. That's all recursion is. There's no fancy secret to it. It's not complicated. It's just a routine that calls itself a limited number of times. Now, with that said, many recursive routines work without having to pass a start or finish point. HOW?? By whatever they're doing being constrained by the program itself in some other manner. A tile program might always start at point (0,0) and increment by 1 until it goes all the way right and down until it runs out of screen space, like this little tile demo below: Code: (Select All) $COLOR:0 Your recursive file access works just like this -- start point is the beginning of the file. increment is by file record. finish is EOF. <-- Start. Stop. Limited recursion. See how simple this stuff is, once you break it down to the BASICs? RE: Why does my Loop end after 11 Loops? - Dimster - 02-12-2023 Thanks for these examples Steve, they are great. I've printed them off and placed them in my Basic Binder. The Looping I have down pat but the jury is still out on if Recursion is a worthy replacement for a Do..Loop/For..Next etc. In some of my formulas I have some pretty deep nesting of the various loops and the results, while accurate take a long time to complete. In one case I had a For...Next nested 6 deep ie For U = 1 to 50 For V = 2 to 51 For W = 3 to 52 etc It damn near took the whole day to complete the calculations. So I'm trying to explore Recursion as a substitute for those standard Do and For loopings. I haven't yet figured the algorythm correctly but on first blush it doesn't appear Recursion can be nested. I can stack Recursion calls but to nest Recursion calls seems I need to set the Recursion call in says a Select Case type algorythm. Not a true nesting but close. I hope Recursion will be as accurate as the nested For loop above but faster. Plus I'm hoping I don't crash. I'm thinking that crashing a recursive routine is dependent on your computer's configuration. I may need to crash to find the limit my computer has with Recursion. RE: Why does my Loop end after 11 Loops? - bplus - 02-12-2023 Quote:but the jury is still out on if Recursion is a worthy replacement for a Do..Loop/For..Next etc. The jury has been back a long time ago, loops are better because recursion can burn through huge amounts of memory not easy to predict, neither is recursion faster. So for serious project like Opening file and processing it you want straightforward method. Recursion is cool and is great learning experience for hobby coder. Recursion does have advantage of doing things like solving Sudoku Puzzles or Mine Sweeping empty cells or just drawing trees, allot easier to code than straight forward methods. The stacking tracks all the different paths your code has to go to cover a situation. Opening and storing a file into data for processing is a single path from start to finish and the most effective way is not recursion. RE: Why does my Loop end after 11 Loops? - Dimster - 02-12-2023 Oh that's devastating news B. I was looking at your Recursion examples and a Quick Sort routine. They are very fast. So here is my For...NEXT test code Code: (Select All) For TNa = 1 To 44 TN stands for Target Number. This is similar to the actual algorythm I am presently using but really stripped down. If you run this code it will take the better part of the day to complete and that's not counting the time the actual algorythm branches off to do some calculations. I have been trying to duplicate this depth of nesting (at least 6 deep) with a recursive algorythm. I am having no luck nesting recursive calls (I don't think Quick Sort is nesting it's recursive calls but rather I think you would call that Stacking recursive calls). So I'm trying to stack them but still fumbling on the code. Impressed with the speed of Quick Sort, I thought it's recursive approach would be a lot faster than For..Next code I'm using. I good idea gone bad, as has been pointed out. RE: Why does my Loop end after 11 Loops? - TerryRitchie - 02-12-2023 I've found recursion to be very useful for path-finding types of algorithms. I used a recursive flood fill in my Minesweeper clone: Code: (Select All) SUB REVEALCELL (x%, y%) Recursive-like routines can also be achieved using a stack as I did in my Super MegaBug program: Code: (Select All) SUB CreateRandomMaze The maze generation code could have been done with recursion as well but a stack did nicely in this case. RE: Why does my Loop end after 11 Loops? - Dimster - 02-12-2023 And the Basic Binder grows recursively. Thanks Terry. I'm into the Super Bowl case of beer now .... planning my next hair brained assault on QB64pe |