int factorial(int n) { int p = 1; while ( n > 0 ) { p = p*n; n = n-1; } return p; } |
Note that as long as n is positive, the body of the while statement is executed.
However, the last action taken in the body is to decrease n's value by one, so eventually the repetition (loop) will stop.
Q: What does factorial return if n is initially zero or negative?
A: