I have some code that I absolutely must implement using goto
. For example, I want to write a program like this:
start:
alert("RINSE");
alert("LATHER");
repeat: goto start
Is there a way to do that in Javascript?
I have some code that I absolutely must implement using goto
. For example, I want to write a program like this:
start:
alert("RINSE");
alert("LATHER");
repeat: goto start
Is there a way to do that in Javascript?
Generally, I'd prefer not using GoTo for bad readability. To me, it's a bad excuse for programming simple iterative functions instead of having to program recursive functions, or even better (if things like a Stack Overflow is feared), their true iterative alternatives (which may sometimes be complex).
Something like this would do:
That right there is an infinite loop. The expression ("true") inside the parantheses of the while clause is what the Javascript engine will check for - and if the expression is true, it'll keep the loop running. Writing "true" here always evaluates to true, hence an infinite loop.
Actually, I see that ECMAScript (JavaScript) DOES INDEED have a goto statement. However, the JavaScript goto has two flavors!
The two JavaScript flavors of goto are called labeled continue and labeled break. There is no keyword "goto" in JavaScript. The goto is accomplished in JavaScript using the break and continue keywords.
And this is more or less explicitly stated on the w3schools website here http://www.w3schools.com/js/js_switch.asp.
I find the documentation of the labeled continue and labeled break somewhat awkwardly expressed.
The difference between the labeled continue and labeled break is where they may be used. The labeled continue can only be used inside a while loop. See w3schools for some more information.
===========
Another approach that will work is to have a giant while statement with a giant switch statement inside:
This is an old question, but since JavaScript is a moving target - it is possible in ES6 on implementation that support proper tail calls. On implementations with support for proper tail calls, you can have an unbounded number of active tail calls (i.e. tail calls doesn't "grow the stack").
A
goto
can be thought of as a tail call with no parameters.The example:
can be written as
Here the call to
start
is in tail position, so there will be no stack overflows.Here is a more complex example:
First, we split the source up into blocks. Each label indicates the start of a new block.
We need to bind the blocks together using gotos. In the example the block E follows D, so we add a
goto label3
after D.Now each block becomes a function and each goto becomes a tail call.
To start the program, use
label1()
.The rewrite is purely mechanical and can thus be done with a macro system such as sweet.js if need be.
In classic JavaScript you need to use do-while loops to achieve this type of code. I presume you are maybe generating code for some other thing.
The way to do it, like for backending bytecode to JavaScript is to wrap every label target in a "labelled" do-while.
Every labelled do-while loop you use like this actually creates the two label points for the one label. One at the the top and one at the end of the loop. Jumping back uses continue and jumping forwards uses break.
Unfortunately there is no other way to do it.
Normal Example Code:
So say the code gets encoded to bytecodes so now you must put the bytecodes into JavaScript to simulate your backend for some purpose.
JavaScript style:
So using this technique does the job fine for simple purposes. Other than that not much else you can do.
For normal Javacript you should not need to use goto ever, so you should probably avoid this technique here unless you are specificaly translating other style code to run on JavaScript. I assume that is how they get the Linux kernel to boot in JavaScript for example.
NOTE! This is all naive explanation. For proper Js backend of bytecodes also consider examining the loops before outputting the code. Many simple while loops can be detected as such and then you can rather use loops instead of goto.
How about a
for
loop? Repeat as many times as you like. Or awhile
loop, repeat until a condition is met. There are control structures that will let you repeat code. I rememberGOTO
in Basic... it made such bad code! Modern programming languages give you better options that you can actually maintain.