Best Practice: Avoid break statement in for loops
807605Oct 6 2007 — edited Oct 7 2007Hello everyone !!
Recently I had a discussion with my architect regarding use of break statements in for/while loops in java.
He suggested, we should avoid break statements in for loops as it works like goto statement. Break means you dont know the terminating condition of the loop and just want to break out of loop, which is bad.
Example without using break statement:
for (int i = 0; i < n; i++)
{
...
if (...)
{
...
i = n;
}
}
Now if I have to agree with my architect, how will I terminate a for loop used in Jdk 5.0 ?
Waiting for your replies.
Thanks and Regards
Rohit.