Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Space Complexity
#1
How do you know if a for loop is using recursion?
Reply
#2
(01-25-2018, 06:51 PM)jwarburton Wrote: How do you know if a for loop is using recursion?

You can tell if any function is using recursion if it calls itself. For example:
Code:
void f(x, y){
    if(x < y){
        printf("%d\n", x);
        f(x+1, y);
    }
return;
}

This example uses recursion because the function f calls itself while x is less than y. So recursion within a for would loop would look something like this:
Code:
void f(x, y){
   for(i = 0; i < y; ++i){
       f(i, x);
   }
    return;
}
In general though, you will not see recursion within a for loop.

Does this answer your question?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)