Unnecessary Goto's

alan at allegra.UUCP alan at allegra.UUCP
Wed Nov 23 03:56:47 AEST 1983


	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



More information about the Comp.lang.c mailing list