You could simplify this slightly by initializing n to 0 and going straight into the while loop.