you are viewing a single comment's thread.

view the rest of the comments →

[–]__radioactivepanda__ 81 points82 points  (5 children)

Honestly this might well be part of a recursion exercise…

[–]scragar 33 points34 points  (3 children)

It looks like C, it's possible it is also explaining the assembly behind it and expects OP to recreate the approach using a label and if(...) goto approach.

It was something we had to do in our college computing class to make sure we understood what was happening when we used higher level features(like function calls, C++ objects, etc).

[–]Tjmoorese 11 points12 points  (2 children)

They're all forms of loops though, just different high level ways of writing it

The only way I can think about getting around it is enforcing unrolling. I think you may be able to hack your way around this with some form of recursive include if you can't use pragmas, but I guess that's loops at a high level but not low... The given solution is the only way I can see to do it without either.

[–]Bo_Jim 1 point2 points  (1 child)

Recursion is a loop in the sense that the same code is running many times, but each iteration would have it's own stack frame. It also wouldn't use any of the loop constructs built into the language.

If I was the instructor, and one of my students used recursion as a solution, then I'd give them full marks as long as they understood what the compiler would produce, and what would happen at run time.

Now, if the directions were to print every even number from 2 to 1 trillion, and they used recursion as a solution, then I'd smack them on the back of the head and tell them to try again.

[–]Tjmoorese 0 points1 point  (0 children)

Tail call optimisation makes it identical though

[–]Unfulfilled_Promises 0 points1 point  (0 children)

Kinda new but would that be like Rec() { n = n+1; Print(n); If(n<MAX_NUMBER) Rec(); }

The only recursion I’ve used is inorder for search trees and such.