[net.lang.c] Unnecessary Goto's

alan@allegra.UUCP (11/22/83)

	I recently had occasion to use a construct which is a little strange,
	and I was wondering whether it's guaranteed to be portable.

			if(0) retry: printf("Try again.\n");
			printf("What's two plus two? ");
			....
			/* if get a bad answer */
			goto retry;

	This code ensures that the "Try again" message isn't executed the
	first time through. It works fine on the VAX (4.1bsd) and PDP-11 (v7).
	Can I be sure that the "goto" to a statement within a condition
	will work with any C compiler?

	Dave Sherman


Beside the portability issue, there is the issue of clarity here.

	do
	{
		printf("What's two plus two? ");
		 ...
		if ( /* bad answer */ )
			printf("Try again.\n");
	} while ( /* bad answer */ );

Now, isn't that much clearer?


	Alan S. Driscoll
	AT&T Bell Laboratories

eric@whuxle.UUCP (12/14/83)

#R:allegra:-202800:whuxle:23200002:000:223
whuxle!npl    Dec  3 15:34:00 1983

While mark's formulation is by far the clearest so far, why not
try this?

	for( goodans = 0; !goodans ; printf("Try again\n") {
		printf("what's 2 plus 2? ");
		.......
		/* code which check results and sets goodans */
	}