Is jumping inside loops legal?

Stephen Clamage steve at taumet.com
Sat Aug 4 04:27:10 AEST 1990


pruss at ria.ccs.uwo.ca (A.R. Pruss) writes:

>I am wondering whether it is legal to jump into a loop, either via
>a direct goto, or via a switch-case construct.

Perfectly legal in ANSI C, and there is an example comparable to
yours (except for one point) in section 3.6.6.1 The goto statement.

The point to watch is that the initializer of the for loop will not
be executed when you jump into the middle, so if you have
	goto middle;
	for(i = 0;  i < max;  i++) {
	    ...
	middle:
	    ...
	}
there is no telling how many iterations will be performed.

"Legal" is not the same as "good", however, and such code is hard to read,
hard to maintain, and may prevent a the compiler from generating good
code.  It is worth your time to study your algorithm and code it in a
more straightforward way.  When I encounter such code written by others,
I usually find that recoding it without tricks results in source code
that is shorter and easier to understand, and in object code which is
smaller and faster.

-- 

Steve Clamage, TauMetric Corp, steve at taumet.com



More information about the Comp.lang.c mailing list