Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Best Practice: Avoid break statement in for loops

807605Oct 6 2007 — edited Oct 7 2007
Hello 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.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 4 2007
Added on Oct 6 2007
8 comments
2,007 views