File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Break statement versus return ?

 
Amandeep Singh
Ranch Hand
Posts: 850
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In above method, I want to go out of the loop and method in 1 shot by using return.
What is recommended approach, is it to use break and then outside loop use return ?
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not touching this with a 10-foot pole. The discussion about using return (and even break and continue in loops) within methods is a highly debated one. I would just return in the loop though.
 
Amandeep Singh
Ranch Hand
Posts: 850
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK then as Rob also suggested, I will go through return loop.
 
Paul Clapham
Sheriff
Pie
Posts: 20169
24
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you break out of the loop, then aren't you going to need some logic to avoid executing the code which comes after the loop?

Perhaps the same logic which caused you to break out of the loop in the first place?

Then that would complicate your method and also require duplicate code. I wouldn't do that.
 
Amandeep Singh
Ranch Hand
Posts: 850
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with you Paul.
I am going to use return statement only instead of break. Using return will break the loop and go out of method also in 1 shot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic