Ever wonder why there's no cool animals out there like unicorns and owlbears?4.15 / 5.00 17,612 Views
A rip-roaring dungeon running adventure!4.01 / 5.00 2,141 Views
Keep calm and shoot down mobsters.3.61 / 5.00 1,585 Views
Why is it poor practice to use a JUMP statement to break out of a FOR-loop? This question appeared in a test a while ago, but my teacher never gave us the answers of that test. Thanks.
At 6/16/13 09:08 AM, FlyingColours wrote: Why is it poor practice to use a JUMP statement to break out of a FOR-loop? This question appeared in a test a while ago, but my teacher never gave us the answers of that test. Thanks.
The simple answer follows:
The framework of a language is organized in a specfiic way to control programming flow.
Jumping to various lines of code disrupts this flow.
Thanks! *memorises answer*
BTW, does that mean I shouldn't use break; in JS and AS?
No break is fine. Break doesn't disrupt flow.
GoTo and JUMP disrupt flow.
At 6/16/13 11:18 AM, PMMurphy wrote: No break is fine. Break doesn't disrupt flow.
GoTo and JUMP disrupt flow.
Thanks again PMMurphy; I'll remember that. :)
These commands are known as assembly language.
They are already implemented in your base functions currently. But they are organized in ways so people don't have to learn how to organize them yourself. I do not know the negative side effects for miss-using assembly language i will learn that when i cover assembly language in the future.
Every single for-loop using a GoTo / JUMP statement in them.
The syntax is just changed and organized in ways that make the computer understand the code easier. When you introduce more of these statements into your code you are causing new and wierd ways to "loops" to occur. That can cause some very hectic and hard to debug un-organized code.
Same goes with break.
But, like goes with anything in programming. You should understand what your commands do and how to use them before you use them. Break; may indeed be a fine command to use, but if it is used incorrectly it is still bad practice.
Same goes with anything period.
Actually, at the risk of being completely wrong. I feel that break is also a disruption of flow.
For me, the reason to avoid GOTO type statements has always been elegance. There was always a way to generalize the algorithm to the extent where the GOTO was superfluous.
In the same way, the condition in your FOR loop, made more sophisticated, could also preclude the need for any break statement within the loop.
A lot of this comes down to personal preference and mincing of terms, but I feel roughly the same way about BREAK as I do about GOTO.
= + ^ e * i pi 1 0
At 6/16/13 09:08 AM, FlyingColours wrote: Why is it poor practice to use a JUMP statement to break out of a FOR-loop?
It's not. It is only poor practice if you use break incorrectly because just like anything in programming it can be abused (just like static members, singletons, inline functions, and regular expressions).
If, for example, you're in a loop and a certain condition is met which makes the loop no longer necessary, but completing execution of the function said loop is running in is still necessary, then it's perfectly fine to use break to exit the loop.
At 6/20/13 04:13 AM, sharpnova wrote: For me, the reason to avoid GOTO type statements has always been elegance. There was always a way to generalize the algorithm to the extent where the GOTO was superfluous.
Completely avoiding a control statement isn't elegant though. More often than not goto is misused, sure, but it does have its place. For example in C goto is very useful for error cleanups. To use a real-world example look at Zed A. Shaw's C debug macros. That is a perfect example of correctly using goto; I don't even want to imagine the spaghetti code which would be required to do all that without goto.
At 6/20/13 04:13 AM, sharpnova wrote: In the same way, the condition in your FOR loop, made more sophisticated, could also preclude the need for any break statement within the loop.
I agree that these can be used in clever ways to make code better. Even GoTo if you know what you are doing can be used well. But the problem is most people don't take the time to analyze what they are doing to do it well, honestly id rather just not use it entirely but that is my preference.
Break on the other hand. There are methods i can write in 6 lines of code with break, that require 30+ lines of code without. So i highly disagree with that.
sharpnova its ok to take a stab at things but the reason GoTo disrupts flow is because the compiler expects to go from line one all the way to line 10. When you loop it with goto and break, you let the compile transition to the next stage of the code in a organized manner.
So in break, you meerly just transition to the next line of code, where as GoTo may skip from line 4, to line 300, to line 240 to line 3 to line 740. Etc etc etc.
It's the skipping that disrupts flow, but break meerly just exits the loop you currently designed breaking off excess code you know you don't need. Where as GoTo jumps everywhere.
Oh im so sorry to anyone reading this i put wrong information. ( i just remembered the correct information).
Your programs are supposed to compile from the smaller valued line of code to the larger. GoTo makes it possible so you can go from higher to lower. Thus GoTo disrupts programming flow.
Where as with break, you always go higher, never lower.