-4

Recently I found the risk when using st like this:

int i = 10;
int sum = 0;
while ( i-- ){
      sum = sum + i;

It actually get sum = 9 + 8 + 7 + .. + 1. So it lacks 10 in total. But I prefer this way of coding, it's fast and professional. Is there any advice to prevent from the risk and still have concise code?

Jk1
  • 11,233
  • 9
  • 54
  • 64
Duc Tran
  • 6,016
  • 4
  • 34
  • 42
  • 14
    Use a for-loop. I fail to see what is particularly "professional" about this. –  Apr 26 '11 at 17:26
  • Doesn't really tell the compiler your intention either, missed opportunities... – hiddensunset4 Apr 26 '11 at 17:28
  • 10
    `while( (sum += i--), i);` is more concise, works correctly, but would probably get you fired. – Pablo Apr 26 '11 at 17:28
  • 3
    `sum = 55; /* sum(1..10) */` is more concise, works correctly, and will get you praised where I work. – Robᵩ Apr 26 '11 at 17:41
  • @silentbang: I bet you'd just love APL. ;-P – Emile Cormier Apr 26 '11 at 17:46
  • 2
    Writing obscure or overly concise code is the opposite of writing clear, maintainable and professional code. Writing professional code is not about showing your colleagues how clever and concise you can write your statements. – Jesper Apr 26 '11 at 21:33
  • @Pablo : is the comma same effect as the symbol "&&" ? When I try "while( sum += i-- , i)" without the bracket it loops forever with strange values of i. if had bracket, it is correct as you mentioned – Duc Tran May 01 '11 at 12:18
  • @silentbang according to operator precedence rules, both forms should be equivalent, but I guess its easy for a compiler (specially older ones) to mess up the evaluation order. That's why its a good idea to specify precedence manually using the brackets. Although its a better idea to avoid such easy-to-confuse expressions. As to what it means: `A, B` means "evaluate expression A, discard the resulting value, then evaluate expression B and use its value for the whole expression" – Pablo May 02 '11 at 04:25

5 Answers5

5

You have a counter, a stop-condition and a decrement operation, so use a for loop - it's a much better fit than while:

int sum = 0;
for (int i = 10; i > 0; --i) {
    sum += i;
}

"Professional", concise and risk-free :)

Edit: Or if you want to be really concise:

int sum = 55;
GrahamS
  • 9,980
  • 9
  • 49
  • 63
3

At least for this specific type of series (sum from 1..N) you can just do N*(N+1)/2. 10*11/2 = 55.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

Postfix increment/decrement can be pretty nasty. I recommend not using it. Your example isn't even the worst of it. It's behaving pretty well: you're actually getting sum = 9+8+7+...+1+0; So you are going through the loop 10 times as one would think.

As mentioned in comment, use a for-loop.

int sum=0;
for (int i=10;i;--i) sum+=i;

The prefix operator is much less confusing, and in some cases, makes faster code.

JCooper
  • 6,395
  • 1
  • 25
  • 31
0

Use do-while instead of while.

Vink
  • 1,019
  • 2
  • 9
  • 18
0

No idea what you mean by "risk" and "professional" way of coding, this code is just wrong. If you want it to look "similar" to what you wrote

int i = 10;
int sum = i;
while ( i-- ){
      sum = sum + i;

or

int i = 10;
int sum = 0;
do {
    sum += i;
} while (--i);
Marek Sapota
  • 20,103
  • 3
  • 34
  • 47
  • The `do-while` construct will end up executing the loop body 11 times, won't it? Hard to say what counts as 'risky' in OP's world, but it seems dangerous to me. – JCooper Apr 26 '11 at 17:35
  • Should execute 10 times after the edit. You can still count it as risky - nasty things if `i` is not positive, etc. Hard to make the best call without a context. – Marek Sapota Apr 26 '11 at 17:39