Tag Archives: goto

Hoppa Goto Style!

In 1996 I was 12, that very year I learnt my first language ever: gwbasic

Nobody taught me it, instead at the end on my school book I found some mention of it and I start studying it by myself.

I figure out how it worked and learnt all the command described in the book:

REM (for comments)

IF THEN ELSE

CLS (clear screen)

PRINT

INPUT

GOTO

Cool, all those new stuff, I wanted to try to make a program and decided to write down (on paper, no computer available back then) a program to solve all the trigonometrical problem I had, basically choosing the number of angles and then deciding which value I had and compute the rests.

I end up writing pages just for triangles, anyway I pretty much forgot about it once finished because I didn’t had a computer to try it out.

One year later I got my first pc: an used IBM 286 (which btw I later discovered it had a basic interpreter in the rom)

Anyway one day while cleaning my room I found out my old source list and I end up trying my program, it took hours to just copy it onto the my first elaborator because of my awful writing speed at the time, but once done I try running it and it worked perfectly.

I still remember parts of that source, done basically all with print, input and goto (and pen and paper).

Imagine my surprise some years later when I discovered about loops and how much less typing I could have done. oh well.

Anyway this post is not about recall those days but to introduce you to a brand new old style of programming, the goto one!

btw 1996 was also the year JavaScript first appeared (yep I know it was 95, but it was LiveScript back then).

In ECMAScript 262 3rd Edition (the current most implemented and supported version of JavaScript) Goto is defined as a future reserved word and should not be used for this reason, as far as I know goto has never been implemented in any javascript implementation, but worry not!

Even if goto has not been implemented labels have!

Thanks to labels we have a good but slightly different approximation of goto,

The biggest difference is that we have to use two different keywords in we want to jump forward or backward.

So here’s the trick, to jump backward we can place a label and use the continue keyword to jump to that label,

continue need to be inside a loop though, so we’ll have to place a for(;;) or a while(true) around the code we have to jump back to.

So the classic hello world in basic is

10 print(“hello, World!”)

20 goto 10

Following what I told you earlier we can translate it in JavaScript like so:


Ten: for(;;){
  console.log(“hello, World!”)
  continue Ten
}

Obviously in this case both continue or continue Ten will do the same thing

If you want to test this code try this code, remember that it’s inside an infinite loop, so you have to break out that loop like I’ve done here: http://jsfiddle.net/kentaromiura/CZ2fg/

Ok, continue will work ok as long as it’s inside a loop and as long we jump to some previous point in the code, but what if we want to jump forwards?

Let’s look at some classical basic code like this:

10 I = 5

20 if I = 0 goto 60

30 print (“hi”)

40 I–

50 goto 20

60 print (“wow”)

The trick to jump to line 60 is to using the break keyword!


var i=5
Twenty: for(;;){
  if(i==0) break Twenty
  console.log(‘hi’)
  i--
  continue Twenty
}
console.log(“wow”)

http://jsfiddle.net/kentaromiura/52eAk/

Or better:


Sixty:{
  var i=5
  Twenty: for(;;){
    if(i==0) break Sixty
    console.log(‘hi’)
    i--
    continue Twenty
  }
}
console.log(“wow”)

http://jsfiddle.net/kentaromiura/CEM4E/

As you can see break will enable us to do a forward jump and the cool thing is that break don’t even needs to be inside a loop as continue, so instead so we can write it like in our examples and assigning the label to a simple block statement.

So while goto is not directly available in JavaScript the truth is that is has always been available but nobody told you that because they fear the power that can arise from this.

Advertisements
Tagged , , , ,