Assignment in Conditionals

COTTRELL at BRL.ARPA COTTRELL at BRL.ARPA
Fri Aug 9 03:21:12 AEST 1985


/*
> 3) Is there really much to be gained by using assignment within
>    a boolean expression, for example, is
>
>	if ((fp = fopen("foo", "r") == NULL) {

Darn tootin! Consider the common case of:

	while ((c = getchar()) != EOF) {
		process(c);
	} /* AT EOF */

Now how do you write it without the assignment?

	1)	c = getchar();
		while (c != EOF) {
			process(c);
			c = getchar();
		} /* AT EOF */

	2)	for (c = getchar(); c != EOF; c = getchar()) {
			process(c);
		} /* AT EOF */

	3)	do {	c = getchar();
			if (c != EOF)
				process(c);
		} while (c != EOF);

	4)	for(;;) {
			c = getchar();
			if (c == EOF)
				break;
			process(c);
		}

Get the idea? Either 1) you have to duplicate the initial assignment,
2) you have to duplicate the test, or 3) you have to use TWO control
struxures (three if you count break) in a non-obvoius way where one
would do. This is a very simple example, but things get quite 
complicated if either the assignment or the processing is expanded 
inline.

There is no reason why assignment should not return a value. LISP & APL
both do. An experienced programmer in either language can handle that.
In LISP, even conditionals & loops return values, & I see no reason why
they couldn't have been designed that way in C.

The penalty for excluding assignment in conditionals is contortions
in the control struxures.

If you can't stand the heat, stay out of the compiler.
Stroll around the grounds until you feel at home.

	jim		cottrell at nbs
*/
------



More information about the Comp.lang.c mailing list