• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

calling 'return' doesn't seem to work immediately?

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a method and inside of an if/then, I have it print the word "done" and then I call return. Problem is that is prints "done" about 100 times instead of exiting the method right away. It does eventually work, but why does it print "done" so many times first? It almost looks like a loop...
 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it does sound like a loop, yes. could you post the code?
 
Max Simpson
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
doesn't calling 'return' cause the method to exit immediately, regardless of whether or not it is called from within a loop?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I too, would like to see the code for this.

But to answer your question... there is a case, where it doesn't return immediately. If there is a finally clause, that code needs to be executed.

Henry
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AS Henry says, there is a case where calling return does not exit the function right away. However, in all other cases, return returns, terminating the flow of the method -- even inside a loop.

In order to accurately explain what is going on, we will need to see your code, butI think that you have a loop somewhere that you are not accounting for.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Max Simpson:
doesn't calling 'return' cause the method to exit immediately, regardless of whether or not it is called from within a loop?



As others have said, there are cases where "return" doesn't return right away. But I doubt that's the problem here.

It sounds like you might be confused on what "return" returns from. It will cause the method to exit, but it's the method that the return is in. In other words, your function that prints "Done" is what the return will exit from. And when it exits, it winds up right back at the loop your function was called from. That method (the one with the loop) is not at all affected by the return that's in your "Done" method.

- Jeff
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As the others have said, posting code snippets of the loop and of the method that has the return statement would help us to help you.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post the exact code that is causing this behavior. Without the exact code, we can only guess at what is happening here.

Layne
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic