Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84 exptools; site ihuxe.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!mhuxn!ihnp4!ihuxe!foss From: foss@ihuxe.UUCP (foss) Newsgroups: net.lang.c Subject: Re: C Style Message-ID: <1217@ihuxe.UUCP> Date: Fri, 13-Sep-85 09:16:32 EDT Article-I.D.: ihuxe.1217 Posted: Fri Sep 13 09:16:32 1985 Date-Received: Sat, 14-Sep-85 06:24:47 EDT References: <180@chinet.UUCP> Organization: AT&T Bell Laboratories Lines: 62 > > 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 ----------------------------------------------------------------