Be a Supporter!

Jump statements question

  • 232 Views
  • 10 Replies
New Topic Respond to this Topic
FlyingColours
FlyingColours
  • Member since: Jul. 3, 2011
  • Offline.
Forum Stats
Member
Level 06
Programmer
Jump statements question Jun. 16th, 2013 @ 09:08 AM Reply

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.

PMMurphy
PMMurphy
  • Member since: May. 27, 2012
  • Offline.
Forum Stats
Member
Level 01
Programmer
Response to Jump statements question Jun. 16th, 2013 @ 10:18 AM Reply

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.

FlyingColours
FlyingColours
  • Member since: Jul. 3, 2011
  • Offline.
Forum Stats
Member
Level 06
Programmer
Response to Jump statements question Jun. 16th, 2013 @ 11:15 AM Reply

Thanks! *memorises answer*

BTW, does that mean I shouldn't use break; in JS and AS?

PMMurphy
PMMurphy
  • Member since: May. 27, 2012
  • Offline.
Forum Stats
Member
Level 01
Programmer
Response to Jump statements question Jun. 16th, 2013 @ 11:18 AM Reply

No break is fine. Break doesn't disrupt flow.

Only
GoTo and JUMP disrupt flow.

FlyingColours
FlyingColours
  • Member since: Jul. 3, 2011
  • Offline.
Forum Stats
Member
Level 06
Programmer
Response to Jump statements question Jun. 16th, 2013 @ 11:26 AM Reply

At 6/16/13 11:18 AM, PMMurphy wrote: No break is fine. Break doesn't disrupt flow.

Only
GoTo and JUMP disrupt flow.

Thanks again PMMurphy; I'll remember that. :)

PMMurphy
PMMurphy
  • Member since: May. 27, 2012
  • Offline.
Forum Stats
Member
Level 01
Programmer
Response to Jump statements question Jun. 16th, 2013 @ 11:30 AM Reply

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.

sharpnova
sharpnova
  • Member since: Feb. 19, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to Jump statements question Jun. 20th, 2013 @ 04:13 AM Reply

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

Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Jump statements question Jun. 20th, 2013 @ 01:18 PM Reply

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'll use my one code as an example here. Take a look at my each() function I wrote for my JavaScript framework. It behaves pretty much the same way jQuery.each does, and allows the user to end iteration by returning a literal false. How would you achieve this behaviour without the break keyword?

PMMurphy
PMMurphy
  • Member since: May. 27, 2012
  • Offline.
Forum Stats
Member
Level 01
Programmer
Response to Jump statements question Jun. 20th, 2013 @ 08:17 PM Reply

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.

PMMurphy
PMMurphy
  • Member since: May. 27, 2012
  • Offline.
Forum Stats
Member
Level 01
Programmer
Response to Jump statements question Jun. 20th, 2013 @ 08:28 PM Reply

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.

PMMurphy
PMMurphy
  • Member since: May. 27, 2012
  • Offline.
Forum Stats
Member
Level 01
Programmer
Response to Jump statements question Jun. 20th, 2013 @ 08:37 PM Reply

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.