break or return?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

break or return?

 
0
  #1
Mar 4th, 2009
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?
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,878
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: break or return?

 
0
  #2
Mar 4th, 2009
You're talking about c++?
The answer is yes:
break will only break out of the 'most nested loop' . For example:

  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:
  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 960
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is online now Online
Posting Shark

Re: break or return?

 
0
  #3
Mar 4th, 2009
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.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: break or return?

 
0
  #4
Mar 4th, 2009
> then tells the OS to return back to the called procedure.
The OS isn't involved in such things.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: break or return?

 
0
  #5
Mar 4th, 2009
thanks niek, they will make the same effect if there is only one level of loop, not nested, as a conclusion..
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,878
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: break or return?

 
0
  #6
Mar 4th, 2009
Originally Posted by serkan sendur View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: break or return?

 
0
  #7
Mar 4th, 2009
Originally Posted by niek_e View Post
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.
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 46
Reputation: kbshibukumar is an unknown quantity at this point 
Solved Threads: 7
kbshibukumar kbshibukumar is offline Offline
Light Poster

Re: break or return?

 
0
  #8
Mar 5th, 2009
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).
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,728
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: break or return?

 
1
  #9
Mar 5th, 2009
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 46
Reputation: kbshibukumar is an unknown quantity at this point 
Solved Threads: 7
kbshibukumar kbshibukumar is offline Offline
Light Poster

Re: break or return?

 
0
  #10
Mar 5th, 2009
Originally Posted by Narue View Post
>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?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC