C style

COTTRELL, JAMES cottrell at nbs-vms.ARPA
Wed Sep 18 08:37:21 AEST 1985


/*  Doug gwyn's version
> There are several other non-GOTO ways to rewrite the first example:
> 
> 	move( 4, 10 );			/* position cursor */
> 	refresh();
> 
> 	valid = false;
> 	while ( !valid )
> 		switch ( ch = getch() )	/* validate input char */
> 			{
> 		case 'E':
> 		case '1':
> 		case '2':
> 		case '3':
> 		case '4':
> 		case '5':
> 			valid = true;
> 			break;
> 
> 		default:		/* not valid */
> 			putchar( BELL );
> 			}
> 
> 	addch( ch );			/* echo valid input char */
> 	refresh();
> 
> Although this introduces an additional (Boolean) variable, which is
> one of the problems with goto-less programming, the variable has
> very clear meaning and may actually help in understanding the code.

Axually, you don't need the additional boolean. I'm leaving out
the stuff before & after the while because I know we can code sequences.

 	while (1) {
 		switch (ch = getch()) {	/* validate input char */
 		case 'E':
 		case '1':
 		case '2':
 		case '3':
 		case '4':
 		case '5':
 			break;
 		default:		/* not valid */
 			putchar(BELL);
 			continue;
		}
		break;
	}

I will concede that this starting to become a rat's nest tho.
How about this one:

	do {	switch (ch = getch()) {
 		case 'E':
 		case '1':
 		case '2':
 		case '3':
 		case '4':
 		case '5':
			break;
		default:putchar(ch = BELL);
		}
	} while (ch == BELL);

> > I guess what I'm really asking is:
> > 	If you had to modify this program, written by someone else who
> > 	commented it sparsely, which style would you prefer to work on?
> 
> Mine, of course.

You got the right answer here! Everyone likes their own style best.

Oh. And put the braces IN THE RIGHT PLACE!!! Do it K&R style.
Any other way is fighting a losing battle. You can't beat us. Join us.

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



More information about the Comp.lang.c mailing list