Paul said:
The only difference I see is in behavioral latitude (e.g. a question of
degree, not type). "goto" can go anywhere, "break" and "continue" can only
jump out of the immediate enclosing control structure, unless they are
accompanied by a label, then they can go nearly anywhere, which pretty much
makes them a substitute for "goto".
That's not the case, though. It makes their behavior a little more
flexible, but still constrains them to be used only for the purpose of
exiting blocks. For example, the following C code could not be
rewritten in Java with even a labeled break or continue:
int i = 41;
goto insane_location;
for (i = 0; i < 5; i++)
{
j = get_valid_value_for_j();
insane_location:
do_something_with(j);
}
Not strictly speaking true. You can put destination labels nearly anywhere.
Yes, actually, it is strictly true. You can put a label prior to any
statement, but it's not a destination label; it's a name for the
statement (which is probably, though not necessarily, a compound
statement). A labeled break/continue that uses a label must occur
within the statement that's labeled.
This example actually compiled without error:
public class Test {
public static void main(String[] args)
{
labela: {
int x = 3;
labelb: while(true) {
labelc: if (x == 0) {
break labela;
}
else if(x == 1) {
break labelb;
}
else break labelc;
}
}
}
};
There's some confusion going on here because of the scope of the
statements. Remember that an if/else is a single statement, and that
chained if/else statements resolve to:
if (firstCondition)
{
...
}
else
{
if (secondCondition)
{
...
}
else
{
et cetera
}
}
So your 'labelc' actually applies to the entire contents of the while
loop, and the 'break labelc;' just causes you to quite trying to do the
contents of the while loop. If you intended to break only out of the
conditional block of the if statement, then you should put your label
directly prior to the open-brace of that block. In any case, you
haven't written a single break or continue that enters a block at
multiple points or jumps around arbitrarily -- you have instead only
used them to leave a block, which is their purpose.
The important thing to remember at this point is that any algorithm that can
be created using break/continue and labels can also be created without
them.
The same thing is true of loops and conditionals, which could easily be
replaced with recursion and polymorphism. However, I wouldn't want to
program without them. The thing to remember is not whether it's
possible to without break/continue, but rather whether they are
worthwhile tools for programming.
Writing code without break or continue often results in more levels of
nesting, more -- and more visible -- loop and conditional structures,
and the introduction of more local variables to hold temporary flags
that are only there to get the control flow right. These are not
benefits to be aimed for; they increase the complexity of code.
I personally think the inclusion of break/continue was made after a failure
of nerve at Sun, who wanted Java ot be adopted by people not terribly crazy
about, or particularly adept at, structured programming.
Yes, you seem to have a number of strong opinions. This one isn't very
viable, though, and you may be well-advised to drop it. Why would Sun
go out of their way to design and implement a new extension to a
language feature, when they've only included it as a concession to non-
structured programmers? No, it's clear that the engineers at Sun
considered break and continue to be acceptable structured alternatives
to many uses of goto, and strengthened them so that they could handle
more situations than they could before, but still in a structured way.
What is surprising is this idiom apparantly doesn't occur to students
naturally. I noticed this in C and C++ programming discussions also, in
years past. It seems to be accepted as a last resort, after every
alternative has been found wanting.
Yep. The reason for this is that in the course of trying to teach
students to write readable code, many universities have began
promulgating the idea that code should be verbose. Using the expression
result of assignment operators in C-family languages is one of those
things that is under attack -- so even universal idioms that do so are
pressured to be abandoned. Such folks would be much better advised to
spend their time thinking about human factors in reading code, rather
than wasting their time attacking low-level language constructs.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation