Purpose
Most programming languages provide for, while, recursion loop statements. Which one is best?
for
- I know how many repeat needed
- Cannot repeat with certain condition (need to use if and break)
- Hard to use previous result to next repeated loop
- Good usage example : print numbers in array.
while
- I do not know how many repeat needed
- repeat with certain condition
- Hard to use previous result to next repeated loop
- Good usage example : count how may words you typed
recursion
- I know how many Stack Memory that will use
- repeat with certain condition
- Easy to use previous result to next repeated loop
- Good usage example: find 88th’s Fibonacci number
additional considerable conditions
- Does your programming language provide tail recursion
- Program environment such as embedded, Android, or Web