Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.1 6/24/83; site mmintl.UUCP
Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!genrad!panda!talcott!harvard!cmcl2!philabs!pwa-b!mmintl!franka
From: franka@mmintl.UUCP (Frank Adams)
Newsgroups: net.lang.c
Subject: Re: C Style
Message-ID: <662@mmintl.UUCP>
Date: Mon, 16-Sep-85 20:08:22 EDT
Article-I.D.: mmintl.662
Posted: Mon Sep 16 20:08:22 1985
Date-Received: Fri, 20-Sep-85 06:33:47 EDT
References: <180@chinet.UUCP>
Reply-To: franka@mmintl.UUCP (Frank Adams)
Organization: Multimate International, E. Hartford, CT
Lines: 72
Keywords: which is better?
Summary: None of the above


In article <180@chinet.UUCP> rlk@chinet.UUCP (Richard L. Klappal) writes:
>The question:
>	Which of the following code segments is more understandable,
>	(readable, structured, etc) given the current attitudes
>	about the presence of goto's in programming?
>
>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();

Leaving aside the fact that you left out some statements in the second
version, I would not use either of these styles.  I don't like either gotos
or side effects in conditionals.  I would use something like one of the
following:

	noecho();		/* turn off echo */
	for (;;) {
	   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 */
	   }
	   else {
	      break;
	   }
	}
	addch(ch);		/* echo the valid character */
	refresh();		/* update screen */
	. . .

or

	noecho();		/* turn off echo */
	havech = FALSE;
	while (! havech) {
	   move(4,10);		/* set cursor position */
	   refresh();		/* update screen */
	   ch = getch();	/* get input character (without echo) */
	   havech = (ch >= '1' && ch <= '5') || ch == 'E';
	 			/* valid input is 1 thru 5 or 'E' */
	   if (! havech) {
	      putchar(BELL);	/* sound bell on invalid input */
	   }
	}
	addch(ch);		/* echo the valid character */
	refresh();		/* update screen */
	. . .


Frank Adams                           ihpn4!philabs!pwa-b!mmintl!franka
Multimate International    52 Oakland Ave North    E. Hartford, CT 06108