C Style

foss foss at ihuxe.UUCP
Fri Sep 13 23:16:32 AEST 1985


> 
> The question:
> 	Which of the following code segments is more understandable,
> 
> GOTO VERSION:
> 	. . .
> 	noecho();		/* turn off echo */
> retry:
> 	move(4,10);		/* set cursor position */
> 	refresh();		/* update screen */
> 	ch = getch();		/* get input character (without echo) */
> 	if ((ch < '1' || ch > '5') && ch != 'E')
> 	{			/* valid input is 1 thru 5 or 'E' */
> 		putchar(BELL);	/* sound bell on invalid input */
> 		goto retry;
> 	}
> 	addch(ch);		/* echo the valid character */
> 	refresh();		/* update screen */
> 	. . .
> 
> versus NO GOTO VERSION
> 
> 	for ( ; (((ch=getch()) < '1' || ch > '5') && ch != 'E') ; )
> 		putchar(BELL);
> 	addch(ch);
> 	refresh();
> 
----------------------------

Enable Flame Thrower:

It is a tie. Both versions are lame and don't use 
the most appropriate construct avaliable.  
	-> Goto's are for teen-agers who play video games 
	   (and grow up to play D & D). 
	-> With the 'for' loop, the general idea is:

	   for( initial condition; final condition; change condition )
		foo;

	   Unfortunately, the 'for' loop above has no 
	   initial condition and no change condition.
.............................

I suggest a more appropriate construct, the 'while' loop:

	while(( ch =  getch()) != EOF )
	{
		if(( ch < '1' || ch > '5') && ch != 'E')
			putchar( BELL );
		addch( ch );
		refresh();
	} /* end while */

This IS more readable and demonstrates what 
really needs to be done by the loop.

Disable Flame Thrower:

----------------------------------------------------------------
Raymond M. Foss -> AT&T Bell Laboratories -> ..!ihnp4!ihuxe!foss
----------------------------------------------------------------



More information about the Comp.lang.c mailing list