• Post Reply Bookmark Topic Watch Topic
  • New Topic

Return Statement in For Loop  RSS feed

 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it okay to return from a method in a for loop?



Is there any reason to provide a break statement?

Thanks,

Drew

[ January 08, 2008: Message edited by: Drew Lane ]
[ January 08, 2008: Message edited by: Drew Lane ]
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if that is illegal or if it even works lol, but I know that you can have a boolean and just break if the condition is true, and then return that boolean value at the end of the function.

Justin Fox
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works fine. Professional programmers tend to have strong feelings about this question. Some think that a method should only ever have one return statement, and therefore think a break and a flag would be good here; other people see no problem with multiple returns and think the flag is just clutter. Personally, I'm in the second camp.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!