/    Sign up×
Community /Pin to ProfileBookmark

GOTO Statements

I am new to javascript. I reached a point in my programming where I need a GOTO Statement. This may sound naive, but DO they Exist? If so please tell me where I can find the statement format.
Thank you,
tgw

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@so_is_thisDec 28.2006 — Nope, they don't exist. Time to learn structured programming skills. ?
Copy linkTweet thisAlerts:
@felgallDec 28.2006 — Javascript supports structured programming and object oriented programming. It doesn't support spagetti programming and so has no need for a goto statement.

I haven't used a goto statement in programming in any language in over 20 years and even back when I did use it the only reason was to allow for structured programming processing that the particular language didn't yet support properly.

If you tell us what you are trying to do that you think needs a goto statement then someone can let you know how it should be coded.
Copy linkTweet thisAlerts:
@toicontienDec 28.2006 — Instead of a GOTO, you probably want a function.
if (action == 1)
goto 15

Could be rewritten as:
if (action == 1) {
doAction1();
}
...

function doAction1() {
alert('You choose action number 1!!');
}
Copy linkTweet thisAlerts:
@coasterAug 08.2009 — I realize this is a very old thread, but it popped up near the top of the first page on a google search for "javascript goto" Now, I realize goto is frowned on, and I agree there's seldom a good case for using one. (and I may have used all of one or two in the last 10 years) However, never say never, and even Donald Knuth says goto's are occasionally useful, and acceptable. And if a guy who practically invented structured programming says that, and who has written more books about it than I could ever hope to read in my lifetime says that, it's good enough for me.

Anyhoo......on to the real reason for dredging up this thread: I have a case where a goto in javascript might be handy ... [b]or a decent substitute[/b] ... and that's terminating during debugging without having to come back up out of several nested functions. A label only works within its local scope, so that's not an answer.

What's the best way to terminate, something on the order of:

if(!confirm("continue?")) { stopAllJavascriptRightNowandDumpOuttotheEditor }

throwing an error works, but it's kinda messy......
Copy linkTweet thisAlerts:
@Jeff_MottAug 08.2009 — Why is [font=courier]throw[/font]ing messy?
Copy linkTweet thisAlerts:
@rnd_meAug 08.2009 — it's not truly available; you will have to dynamically generate new functions from the String(oldFunction) version, inserting/weaving in extra lines of code with breakpoints, gates, conditionals, and flow code.

you might be able to do something less ambitious, but along the same line.

loops provide label'ed continues, so if you make every section a single-iteration loop, you can continue to any of your arbitrary loop headers.


there is also the possibility of creating your own state object, almost like the interpreters. that should let you pause, continue, resume, etc.

you can even close some aspects of the context using eval and with, which you will need to generate your new functions if you want to preserve closures and "this"...

i was going to implement line numbers in my new class faker, but it makes for slow code, sacrificing the potential rare gains in speed that well-placed gotos can provide.
Copy linkTweet thisAlerts:
@coasterAug 09.2009 — Why is [font=courier]throw[/font]ing messy?[/QUOTE]

Ha ha LOL*

Oh, I dunno. Just don't like seeing all that red, I guess. ?






*how come this forum has no laughing smilies?
Copy linkTweet thisAlerts:
@coasterAug 09.2009 — loops provide label'ed continues, so if you make every section a single-iteration loop, you can continue to any of your arbitrary loop headers..[/QUOTE]
This sounds interesting, but a label's scope is local, is it not? How could I use a label when I'm several layers deep in function calls? I guess that's my biggest complaint right now, and why I went to google to search for a js "goto"

The techniques in your first paragraph pretty much describe what I'm doing now, and I'll tell ya, it's a heck of a lot of editing!!

The stuff in the last paragraph I can't even pretend to come close to being capable of. ?
×

Success!

Help @tgweck spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...