wood burning stoves 2.0*
The moose likes Java in General and the fly likes Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Doubt" Watch "Doubt" New topic
Author

Doubt

Vallabhaneni Suresh Kumar
Ranch Hand

Joined: Mar 01, 2004
Posts: 68
Will there be compilation error, if a return statement is encountered with a for loop in Java. Is this a good practice to return within a for loop without breaking on a condition and then return a value.

Thank you
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Have you tried it to see if it will compile?

Layne


Java API Documentation
The Java Tutorial
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"Doubt" is not a very useful topic name. At the JavaRanch, we like it when you UseAMeaningfulSubjectLine

thanks,
Dave
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
Originally posted by Vallabhaneni Suresh Kumar:
Will there be compilation error, if a return statement is encountered with a for loop in Java. Is this a good practice to return within a for loop without breaking on a condition and then return a value.

Thank you


There wont be any compilation error but once you return remaining for loop wont be executed. In some scenario this is kind of code optimization .
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Many folks suggest you allow only a single return from a method never use "break" to exit a loop. They make pretty good arguments, but I find them less compelling the smaller the method is. If the whole thing is less than 10-12 lines it's hard to say an early return or break makes it unreadable.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt
 
Similar Threads
WA #1.....word association
Break statement versus return ?
WA #2 ..... word association
return in catch block?
How to get last ten years in one Array based on Current Year