0
Do you guys have any particular techniques you use to follow program?
I think I am pretty good at following the code and understanding it general (especially when there are comments). I struggle a lot when I come across functions with in functions especially when the same function calls its self. Or if there is a for loop within the for loop. I always tend to get lost when I am trying to run the code in my head.
4 Answers
+ 7
Glad you found a solution :)
+ 6
When the same function calls itself (A.K.A recursion), there is always a terminating block; if not, it will run infinitely and mess with the stack...
example
function call(int a)
if a <= 1 do
return 1
else
call (a - 1)
end
end
So we pass in a parameter, say 3
3 is not <= 1
so we call the function again, this time the argument is 3 - 1, or 2
See we are getting to 1, and as soon as we get to 1, we return 1 and terminate this recursive function.
As for nested for loops, always go to the deepest one (unless an if statement or something prevents it from being run). The most inner loop will do its job first, and once done, call the outer loop. This can be done x many times, x being the level of nested loops.
When I examine code, I have pencil and paper and write down every variable i need. For loops tend to have i - l or x - z (int i, int j, var i, var j, etc.) so i write those down and step through the loops and keep track of the values these variables hold.
Tedious task, yes, but it is very helpful :)
No pencil or paper, you can use a debugger, set the breakpoint to the outmost loop, and watch (theres a feature for this) the variable values. This, for me, i dont really like because some debuggers dont allow backtracing
+ 1
thanks for the reply guys. I think I going to try the debugger. I never actually used it. but gonna learn how.
0
honestly it is like following a specific logic. So run throught the code step by step.for example when you start looping u will continue to loop until a specific condition is met. a loop inside a loop same thing u cant go out of the outer function unless the inner is satisfied