From: CBFalconer said:
lb1: while (c1) {
....
while (c2) {
....
if (c3) goto lb1;
....
}
....
}
From: Lawrence Kirby <
[email protected]>
while (c1) {
....
while (c2) {
....
if (c3) goto lb1;
....
}
....
lbl1: ;
}
Note that both samples of code jump from inside a block to somewhere
outside that block. (One jumps out from two blocks, while the other
jumps out from the inner block while remaining in the outer block.)
Neither is *really* jumping backwards. The one that jumps out two
levels is really re-starting the outer block. A restart is not
*exactly* the same thing as a backwards jump, although I'm probably
shaving closely to note any difference here.
There's an advantage to the second method, of never jumping even
seemingly backwards. Consider the following *wrong* code:
while (c1) {
lbl1: ;
....
while (c2) {
....
if (c3) goto lb1;
....
}
....
}
Note that the outer loop is re-started except that the condition for it
is not re-tested here. This is not the same semantics as the first
examples where it's always re-tested after the jump, and horrible bugs
can happen this bad way.
Note: The one thing you should *never* do, not *ever* *ever*, is jump
from outside a loop to inside a loop, or even into a block.
(If anyone can think of a valid example of such, please post now!)
Note that at the machine level, jumping from inside one block to inside
an unrelated block is done all the time, namely when returning from a
function that was called. And if you're working on a really primitive
CPU that doesn't even have a return instruction, so you have to emulate
it by explicitly popping the return address off a stack and using it to
perform an indirect-address jump, then the jump into the calling block
is quite explicit. So my statement above does not apply to
machine-language returns, whether via return instruction or via
emulation of such, OK?
I was thinking a little earlier about how my Distributed Java
instructor said we need to learn how to drive a stick shift to
understand what's really happening before we drive an automatic, so we
needed to manually write SQL via JDBC/ODBC, and manually call servlets,
and manually do RMI, before we could finally drive the J2EE EJB
automatic! If this philosophy were carried to the extreme, we should
all be required to write machine-language code, then assembly-language
code for a machine that doesn't even have a return instruction so we
understand what's really happening, and then write assembly language
code for a regular machine with a fine return instruction, and finally
we can use C or other ordinary programming language. I was thinking how
much fun it'd be to write such a machine/assembly-language emulator for
beginning students to play with. Having it totally interactive with
nice GUI instead of physical toggle switches on front panel would make
it more edudational and less of a pain to use. Any comments from the
rest of you-all? Knuth had something like this in his books, except
there wasn't a Web-accessible emulator to play with, so you can think
of this as a modern version of Knuth's "Mix machine". Hmm, actually
there should be a wider range of options, all the way down below
machine language to state machines that emulate a computer, to logic
gates that build a state machine, to transistor circuits that build a
logic gate. The student could select which level of abstration to work
on at any given time. At the high end of the scale, above
function-calling would be lexical closures which are used to build OOP.
Hmm, should I copy this to comp.programming?