You're pretty much doing the same as Sayzer (comparing with known to be odd / even numbers) -- but the way in which you have it configured to call itself repeatedly until the operation has completed is considered to be recursion. However, normally recursive methods perform the work on the subject and not references.
Trent Jackson
Another recursive method. However this time it uses the unique feature of Variable Rollover. Also, if nothing is found at the end of checking all possible values for a WORD, the outcome must be Zero.
EDIT: This may be premature, I think I left a hole at the value of "65535". StandbyCode:TestNumber as word TestVal as word For TestVal = 65534 to 2 step -2 If (TestNumber + TestVal)= 0 Then LCDOUT "Even" EndProgram Else If (TestNumber + TestVal) + 1 = 0 then LCDOUT "Odd" EndProgram Else Endif Next TestVal LCDOUT "Zero" EndProgram: end
Last edited by SteveB; - 11th June 2008 at 17:26.
I don't consider that to be recursive. To be recursive means to have a procedure (method in Java's case) -- that repeatedly calls itself until the operation is complete.
Kinda like getting a pie that you need to cut up into (n) pieces. Instead of cutting out each piece you cut the whole thing repeadedly until you have (n) pieces.
Trent Jackson
OK, This is better. It has to do one final check after the FOR..NEXT loop.
Code:TestNumber as word TestVal as word For TestVal = 65534 to 2 step -2 If (TestNumber + TestVal)= 0 Then LCDOUT "Even" EndProgram Else If (TestNumber + TestVal) + 1 = 0 then LCDOUT "Odd" EndProgram Else Endif Next TestVal If (TestNumber + TestVal) - 1 = 0 then LCDOUT "Odd" EndProgram Else LCDOUT "Zero" Endif EndProgram: end
Last edited by SteveB; - 11th June 2008 at 17:35.
Recursive example ...
It keeps calling itself until we have arrived at something. In PBP you would of course need to exchange the "public int divideBy(int n)" with a label and swap "divideBy(n)" with a goto.Code:public int divideBy(int n) { num = num / n if(num > 0) divideBy(n); }
Trent Jackson
Hi Trent, I think...
There'r Out of 10's methods. You want's onlyup to 10th.
So should Listing now.
.
Bookmarks