943,594 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2659
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 4th, 2009
0

break or return?

Expand Post »
hi guys, i wonder if there is technically any difference between using break or return in order to quit a loop without reaching its condition?
Similar Threads
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Mar 4th, 2009
0

Re: break or return?

You're talking about c++?
The answer is yes:
break will only break out of the 'most nested loop' . For example:

C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. for (int i = 0; i < 10; i++){
  4. for (int j = 0; j < 10; j++){
  5. if (j == 5)
  6. break;
  7. std::cout << "inner=" << j <<"\n";
  8. }
  9. std::cout << "outer=" << i<<"\n";
  10. }
  11. return 0;
  12. }
will print
inner=0
inner=1
inner=2
inner=3
inner=4
outer=0
inner=0
inner=1
//etc etc etc

As you can see, the break statement only breaks the inner loop when it reaches 4 ( <5 ). Then it start all over because the outer loop isn't done yet.

If you replace the previous code with:
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. for (int i = 0; i < 10; i++){
  4. for (int j = 0; j < 10; j++){
  5. if (j == 5)
  6. return 0;
  7. std::cout << "j=" << j <<"\n";
  8. }
  9. std::cout << "i=" << i<<"\n";
  10. }
  11. return 0;
  12. }
the code will print:
C++ Syntax (Toggle Plain Text)
  1. inner=0
  2. inner=1
  3. inner=2
  4. inner=3
  5. inner=4
And it's done. You've used return, which means that it should return from the current function. In this case the function is 'main' so it quits the program.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Mar 4th, 2009
0

Re: break or return?

break; Just jumps, after an evalution.
return(x); Will probably store the value were ever it feels appropriate, then tells the OS to return back to the called procedure.
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Mar 4th, 2009
0

Re: break or return?

> then tells the OS to return back to the called procedure.
The OS isn't involved in such things.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Mar 4th, 2009
0

Re: break or return?

thanks nick, they will make the same effect if there is only one level of loop, not nested, as a conclusion..
Last edited by Nick Evan; Jan 23rd, 2011 at 4:04 pm.
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Mar 4th, 2009
0

Re: break or return?

thanks niek, they will make the same effect if there is only one level of loop, not nested, as a conclusion..
No. Break will jump out of a loop, return will jump out of a function. So only if your function exists out of only a loop, will they have they same effect.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Mar 4th, 2009
0

Re: break or return?

Quote ...
No. Break will jump out of a loop, return will jump out of a function. So only if your function exists out of only a loop, will they have they same effect.
yeah that was what i wanted to say.
Last edited by Nick Evan; Jan 23rd, 2011 at 4:04 pm.
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Mar 5th, 2009
0

Re: break or return?

However, having multiple exit points for a function makes it difficult to maintain. There is a greater chance that you'll forget to do the clean ups (which will be usually at the end of the function).
Reputation Points: 12
Solved Threads: 8
Junior Poster in Training
kbshibukumar is offline Offline
65 posts
since Jan 2009
Mar 5th, 2009
1

Re: break or return?

>However, having multiple exit points for a function makes it difficult to maintain.
Forcing a single exit point where it doesn't make sense also makes the function difficult to maintain due to added unnatural complexity.

>There is a greater chance that you'll forget to do the clean ups
>(which will be usually at the end of the function).
In well designed code this isn't a problem. Most (if not all) of your "clean ups" should be handled in destructors anyway.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 5th, 2009
0

Re: break or return?

Click to Expand / Collapse  Quote originally posted by Narue ...
>However, having multiple exit points for a function makes it difficult to maintain.
Forcing a single exit point where it doesn't make sense also makes the function difficult to maintain due to added unnatural complexity.

>There is a greater chance that you'll forget to do the clean ups
>(which will be usually at the end of the function).
In well designed code this isn't a problem. Most (if not all) of your "clean ups" should be handled in destructors anyway.
Do you have destructor for a function to clean up the locals inside?
Reputation Points: 12
Solved Threads: 8
Junior Poster in Training
kbshibukumar is offline Offline
65 posts
since Jan 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: SplitContainer in Win32?
Next Thread in C++ Forum Timeline: VC2008 Permission denied





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC