| | |
break or return?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
You're talking about c++?
The answer is yes:
break will only break out of the 'most nested loop' . For example:
will print
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:
the code will print:
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.
The answer is yes:
break will only break out of the 'most nested loop' . For example:
C++ Syntax (Toggle Plain Text)
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; }
inner=0
inner=1
inner=2
inner=3
inner=4
outer=0
inner=0
inner=1
//etc etc etcAs 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)
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; }
C++ Syntax (Toggle Plain Text)
inner=0 inner=1 inner=2 inner=3 inner=4
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
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
•
•
•
•
thanks niek, they will make the same effect if there is only one level of loop, not nested, as a conclusion..
•
•
Join Date: Jan 2009
Posts: 46
Reputation:
Solved Threads: 7
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).
>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.
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.
•
•
Join Date: Jan 2009
Posts: 46
Reputation:
Solved Threads: 7
•
•
•
•
>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.
![]() |
Similar Threads
- use of continue,break,return (C)
- Unexpected return from string comparison function (C)
- Is a variant return type possible in C++? (C++)
- how to return value from shell script to the calllin C program (C)
- return values (C++)
- print return rernus script (Python)
- break statement (Python)
Other Threads in the C++ Forum
- Previous Thread: SplitContainer in Win32?
- Next Thread: VC2008 Permission denied
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






