previous | index | next

The do...while Statement

int factorial(int n)
{
  if (n == 0 ) return 1; // Add this statement
  int p = 1;
  do {
    p *= n;
    n--;
  }
  while ( n > 0 );
  return p;
}

Moral: A do...while loop would not be the best choice for factorial.


previous | index | next