SHOW:
|
|
- or go back to the newest paste.
| 1 | - | // Loop structures can always be expressed as recursion |
| 1 | + | # Loop structures can always be expressed as recursion |
| 2 | ||
| 3 | - | // Here's an example with a simple while loop: |
| 3 | + | # Here's an example with a simple while loop: |
| 4 | - | method() {
|
| 4 | + | def method(): |
| 5 | - | while(condition) {
|
| 5 | + | while condition: |
| 6 | - | do_stuff(); |
| 6 | + | do_stuff() |
| 7 | - | } |
| 7 | + | # return stuff |
| 8 | - | // return stuff |
| 8 | + | |
| 9 | - | } |
| 9 | + | # And its recursive counterpart: |
| 10 | def method(): | |
| 11 | - | // And its recursive counterpart: |
| 11 | + | if condition: |
| 12 | - | method() {
|
| 12 | + | do_stuff() |
| 13 | - | if(condition) {
|
| 13 | + | method() |
| 14 | - | do_stuff(); |
| 14 | + | # return stuff |
| 15 | - | method(); |
| 15 | + | |
| 16 | - | } |
| 16 | + | # Same thing, but with a 'do-while' loop instead: |
| 17 | - | // return stuff |
| 17 | + | def method(): |
| 18 | - | } |
| 18 | + | while True: |
| 19 | do_stuff() | |
| 20 | - | // Same thing, but with a do-while loop instead: |
| 20 | + | if not condition: |
| 21 | - | method() {
|
| 21 | + | break |
| 22 | - | do {
|
| 22 | + | # return stuff |
| 23 | - | do_stuff(); |
| 23 | + | |
| 24 | - | } while (condition); |
| 24 | + | # And its recursive counterpart: |
| 25 | - | // return stuff |
| 25 | + | def method(): |
| 26 | - | } |
| 26 | + | do_stuff() |
| 27 | if condition: | |
| 28 | - | // And its recursive counterpart: |
| 28 | + | method() |
| 29 | - | method() {
|
| 29 | + | else: |
| 30 | - | do_stuff(); |
| 30 | + | # return stuff |