Re: Simple question!
"Alex Blekhman" <xfkt@oohay.moc> skrev i meddelandet
news:OFi00NLpGHA.516@TK2MSFTNGP05.phx.gbl...
I can see that many attribute some inherently evil properties to
`goto' statement. Michael K. O'Neill already posted good article in
this thread. I'd second his opinion. There is nothing wrong with
`goto' per se. All of us have seen horrible code without single
`goto'. This is not that `goto' (or any other language construct)
that makes code bad; it's quite the opposite: bad, stupid coders
produce filthy code (no matter what the language the use for that).
Any feature in a programming language is just a tool. Use it wisely
and you'll get beautiful elegant code. I just can't get all these
rumbles about `goto'.
Because it is an inherently unstructured way to get from one arbitary
point in the program to another? :-)
When 99.9% of the uses are bad, the general advise to students is:
"Don't use it"!
Frank Rubin is simply wrong! Even though he has found the break
statement, he doesn't see how to use it to avoid the goto.
His Pascal code:
for i := 1 to n
do begin
for j :=1 to n do
if x[i, j] <> 0 then
goto reject;
writeln('The first all-zero row is ', i);
break;
reject: end;
could easily have been written (C++ style, 0 based array):
bool is_all_zero_row(int row)
{
for (int col = 0; col != n; ++col)
if (x[row][col] != 0)
return false;
return true;
}
// ...
for (int i = 0; i < n; ++i)
{
if (is_all_zero_row(i))
{
std::cout << "The first all-zero row is " << i << std::endl;
break;
}
}
See Ma, no gotos !
As others have pointed out before, having to break out of multiple
loop levels is a good indication that the code can be refactored.
Having an is_all_zero_row attribute for the matrix seems like a good
idea, if that is an important property of x.
Bo Persson