changing loops (for to while) and (recursive to for)

Reply

Join Date: Jun 2005
Posts: 16
Reputation: mina1984 is an unknown quantity at this point 
Solved Threads: 0
mina1984 mina1984 is offline Offline
Newbie Poster

changing loops (for to while) and (recursive to for)

 
0
  #1
Jul 2nd, 2005
hi
im kind of confused about how to change a for loop into a while loop and how to change a recursive into a for loop and for loop into a recursive, i read books and searched online but it all seems confusing
could somebody please simplify it for me?

thanx in advance.


mina
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 60
Reputation: zyruz is an unknown quantity at this point 
Solved Threads: 5
zyruz zyruz is offline Offline
Junior Poster in Training

Re: changing loops (for to while) and (recursive to for)

 
0
  #2
Jul 2nd, 2005
Is this in general, or do you have a loop that you want to change?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,622
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 714
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: changing loops (for to while) and (recursive to for)

 
0
  #3
Jul 2nd, 2005
>could somebody please simplify it for me?
No, but we can help you to understand it better if you'll do more than offer vague references to general techniques.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: changing loops (for to while) and (recursive to for)

 
0
  #4
Jul 3rd, 2005
Okay. Generally speaking, tail recursion (where the last thing to be executed is the recursive call) can be optimized to some kind of loop. Not necessarily a "for" loop. Here's a step by step process for doing it. We'll use the GCD example that lies in another thread. The greatest common divisor function, when defined recursively, looks like this:

  1. int gcd(int x, int y)
  2. {
  3. if (y == 0) {
  4. return x;
  5. }
  6. return gcd(y, x % y);
  7. }

Now, the recursive call, gcd(y, x % y), can be interpreted as "Go back to the beginning of the function, with the new values for x and y being y and x%y, respectively."

So now we code that manually:

  1. int gcd(int x, int y)
  2. {
  3. top: /* A label, used by the goto. */
  4. if (y == 0) {
  5. return x;
  6. }
  7. int param_1 = y; /* First assign to temporary variables */
  8. int param_2 = x % y;
  9. int x = param_1; /* Then set your 'new' values for x and y. */
  10. int y = param_2;
  11. goto top; /* Goto the top of the function. */
  12. }

And the recursion has now been replaced by a loop. Further optimization can be done. For instance, if you carefully assign in the right order, only one temporary parameter holder is necessary.

  1. int gcd(int x, int y)
  2. {
  3. top:
  4. if (y == 0) {
  5. return x;
  6. }
  7. int tmp = x;
  8. x = y;
  9. y = tmp % y;
  10. goto top;
  11. }

It's not hard to see that this could be written as a while loop, which runs until y == 0:

  1. int gcd(int x, int y)
  2. {
  3. while (y) {
  4. int tmp = x;
  5. x = y;
  6. y = tmp % y;
  7. }
  8. return x;
  9. }

Sometimes, you can't easily get rid of the goto (and then it's not worth eliminating the goto. But if we're going to talk about what is readable programming or not, we might as well leave in the tail-recursion and trust the compiler to optimize it away).

Now you can try to figure out on your own how to convert a loop to recursive form. It is similar to the reverse of this process.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC