CBFalconer said:
You chose an infinite loop, with a test to break out of it. This
doesn't emphasize the exit condition, as opposed to a loop which
describes the exit point. It doesn't have to have an embedded
assignment. Remember, I am not criticizing, I am just
investigating the thought processes involved.
There are pros and cons to each approach. I can read and write
code in either style. I try to conform to the existing style
when I contribute to projects.
I tend to write most of my code for GNU projects; the GNU coding
standards discourage assignments in `if' conditions, and I've
always mentally extended that to looping constructs. Over the
last few years I've begun to realize why it's not really the same
situation, but the habit is ingrained.
On the other hand, when I write code at a particular company
founded by my PhD advisor, I would use a `while' loop with an
assignment, because that coding style is common there. There, I
would write the program something like this:
/*
* sumStdin.c
*
* Brief description:
* Sums integers from stdin and prints the sum to stdout.
*
* Copyright (C) 2005 BigCorp Subsidiary, Inc.
*/
#include <stdio.h>
#include <ctype.h>
/*
* main()
*
* Description:
* Reads integers from stdin and prints their sum on stdout.
*
* Return value:
* Always returns 0.
*
* Side effects:
* I/O.
*/
int
main(void)
{
unsigned total = 0, current = 0;
int c;
while ((c = getchar()) != EOF) {
if (isdigit(c)) {
current = current * 10 + (c - '0');
} else {
total += current;
current = 0;
}
}
printf("%u\n", total + current);
return 0;
}