which is the cleaner way to do this simple while?

Posted by user363834 on Stack Overflow See other posts from Stack Overflow or by user363834
Published on 2010-06-10T18:53:07Z Indexed on 2010/06/10 19:02 UTC
Read the original article Hit count: 133

Filed under:

I'm learning c++ and I want to make clean and readable code and I was wondering which way is better (while is supposed to make the factorial of 9) :

1 -

int main(){
   int i = 1,r = i;
   while (i < 10) {
      r *= ++i;
   }

2 -

int main(){
   int i = 1,r = i;
   while (i < 10) {
      i++
      r *= i
   }

1 may be harder to understand but it's 1 less line, is it worth it? and what about performance. Obviously it wouldn't matter in such a trivial example but it would be a good practice to make fast code from the begining.

© Stack Overflow or respective owner

Related posts about c++