"break <label>" vs. "goto <label>"

Guy Harris guy at rlgvax.UUCP
Sat Jan 12 16:49:15 AEST 1985


> By this reasoning we can quickly reduce ourselves to working with a turing
> machine. A while statement is redundant because we have goto and if 
> statements. "break <label>" reflects a structured concept that mirrors how I
> view a problem being solved; besides it is not as random and clumsy as a goto.

The difference is that writing a "while" loop using "if" and "goto" yields
clumsy and harder-to-read code; replacing the word "break" with the word
"goto" merely makes the program "goto"-less.  A "break" (or a "continue") is
a fairly arbitrary transfer of control; "goto" is, admittedly, more arbitrary,
but all of them require you to read a bit to discover where control is
actually going.

	for (<something) {
		while (<something else>) {
			.
			.
			.
			break foobar;
		}
	}
foobar:

is no less clumsy than

	for (<something) {
		while (<something else>) {
			.
			.
			.
			goto foobar;
		}
	}
foobar:

The only possible merit of labelled "break" and "continue" is that they
can't be misused in the ways that "goto" can.  There are many ways of
writing poor code, and since we can't make "goto" go away in C we're stuck
with that particular way.  We'll have to rely on education to prevent
spaghetti code.  (There are instances where using "goto"s to cause two
separate paths through code to join at common code is *more* readable, say,
than duplicating the code.  Admittedly, in the one recent instance I've
written of this sort, the whole thing should be done using a different
technique of joining those paths, but that's competing with several other
items on my TODO list.)

	Guy Harris
	{seismo,ihnp4,allegra}!rlgvax!guy



More information about the Comp.lang.c mailing list