Equivalent of goto of c++ in Java in a specific scenario
I am replacing a old C++ code with Java:I know there is no equivalent of goto in java as it causes many surprising errors and unintended loop terminations.
C++ code: //I have skipped the actual code
for(//some condition){
if (excl(grds[i],0))
{
//do something
goto breakout;
}
//some more code
breakout:
//rest of the code
}//end of for loop
Java representation:
for(//some condition){
if (excl(grds[i],0))
{
//do something
}
else
{
//some more code
}
//rest of the code
}//end of for loop
I have kept the "some more code" part in the else section so I guess it will function same as the breakout. When the if condition is true it wont go to the else part and rest of the code will be executed as usual. I guess break or continue wont serve the purpose as we need to any way run the rest of the code section for all itera开发者_JAVA百科tions.
Is this the correct way of representing this C++ code in Java?
Yes, that would be the better way — in Java, and in C++.
This is probably the easiest way:
for(//some condition){
if(!excl(grds[i],0))
{
//some more code
}
//rest of the code
}//end of for loop
You really shouldn't be using goto
in C++. As a side note, in Java you can have named blocks that can be used to simulate a goto
:
namedBlock:
{
// some code
if(condition)
break namedBlock;
// some more code
}
// rest of the code
But this is awful practice. Don't do it. Forget I even mentioned it ;)
for(//some condition){
if (!(excl(grds[i],0)))
{
//some more code
}
//rest of the code
}//end of for loop
Edit: after your clarification, the way you did it is fine.
Why can't you have it like this?
In your C++ code, //some more code
is only executed when the condition is false and the //rest
is always executed. So put that in the if.
Yes.
To be more precise, that's the correct best way of representing that code in C++ as well. Why use goto
instead of a simple if
?
This is a hack
for(//some condition){
do { // note this *do*
if (excl(grds[i],0))
{
break; // out of do/while
}
//some more code
}
while(false); // do only once
//rest of the code
}//end of for loop
I'm sure people will hate me for this, but you can get closer to the goto
statement in Java than the if/else example. For instance, you can do:
for(/*some condition*/){
breakout: {
if (excl(grds[i],0)) {
break breakout; //this will jump to the end of the "breakout" block
}
//some more code
}
//rest of the code
}
Of course, this is not an example of good Java coding style, and just because this can be done doesn't mean it should be done. Really you should stick with the if/else.
There are occasions, very rare occasions, when using goto
is exactly the right choice in C++. The example shown is not one of them. This is a goto
that should never have been used, period. A goto
has zero reason for existence if you can eliminate the goto
with no additional variables, no increase in the complexity.
Some valid uses of goto
in C++ can be easily eliminated in Java.
- C and C++ do not have a way to break or continue out of a nested loop. Java does. When the target of a
goto
is immediately before some close brace, change thegoto label
withcontinue label
. When the target of agoto
is immediately after some close brace, change thegoto label
withbreak label
. (The label statement may need to be moved around.) - Some C++ code uses
goto
in lieu ofthrow
for local error handling / local error recovery. C++ exceptions have a pretty stiff performance penalty. If the code is being converting to Java it's kinda obvious that performance is not an issue. Use Java exceptions for this kind ofgoto
.
精彩评论