Advertisement
Vermiculus

Untitled

Apr 30th, 2012
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Loop structures can always be expressed as recursion
  2.  
  3. # Here's an example with a simple while loop:
  4. def method():
  5.     while condition:
  6.         do_stuff()
  7.     # return stuff
  8.  
  9. # And its recursive counterpart:
  10. def method():
  11.     if condition:
  12.         do_stuff()
  13.         method()
  14.     # return stuff
  15.  
  16. # Same thing, but with a 'do-while' loop instead:
  17. def method():
  18.     while True:
  19.         do_stuff()
  20.         if not condition:
  21.             break
  22.     # return stuff
  23.  
  24. # And its recursive counterpart:
  25. def method():
  26.     do_stuff()
  27.     if condition:
  28.         method()
  29.     else:
  30.         # return stuff
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement