break or return?
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?
serkan sendur
Postaholic
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
You're talking about c++?
The answer is yes:
break will only break out of the 'most nested loop' . For example:
int main()
{
for (int i = 0; i < 10; i++){
for (int j = 0; j < 10; j++){
if (j == 5)
break;
std::cout << "inner=" << j <<"\n";
}
std::cout << "outer=" << i<<"\n";
}
return 0;
}
will print
inner=0
inner=1
inner=2
inner=3
inner=4
<strong>outer=0</strong>
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:
int main()
{
for (int i = 0; i < 10; i++){
for (int j = 0; j < 10; j++){
if (j == 5)
return 0;
std::cout << "j=" << j <<"\n";
}
std::cout << "i=" << i<<"\n";
}
return 0;
}
the code will print:
inner=0
inner=1
inner=2
inner=3
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.
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
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.
MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
> then tells the OS to return back to the called procedure.
The OS isn't involved in such things.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
thanks nick, they will make the same effect if there is only one level of loop, not nested, as a conclusion..
serkan sendur
Postaholic
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
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 ofonly a loop, will they have they same effect.
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
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.
serkan sendur
Postaholic
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
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).
kbshibukumar
Junior Poster in Training
65 posts since Jan 2009
Reputation Points: 12
Solved Threads: 8
>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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>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?
kbshibukumar
Junior Poster in Training
65 posts since Jan 2009
Reputation Points: 12
Solved Threads: 8
>Do you have destructor for a function to clean up the locals inside?
Functions don't have destructors, but you can get the same effect by wrapping resources that need to be cleaned up within a class. This is called RAII, and it's a key concept in good C++ design.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401