Do you believe that one day the GOTO statement will disappear ?? RRS feed

  • General discussion

  • the GOTO statement was used very frequently in the beginning of the BASIC language, in the 1980s.

    This statement is still under Small Basic but very little used, or why do you use it ? in which circumstances ?

    I start the discussion!

    What was the problem with GOTO, was it better to use GOSUB ?.

    me, personally, I use more and more SUB statement in my program, because find better when the clarity of writing to more easily point an error in programming.

    Here is an example of program in 1983, for the ZX-81. ( found in old prog.books)

    on line 170 tell that until we do not press a keyboard key, the program loop indefinitely on line 170, so we have to use CTRL+BREAK to stop running, !!!

    and if we type a key, the program then jump to line 190,  then GOTO line 10 and run again, so another no end loop, until we press CRTL-BREAK to stop running.

    Sinclair ZX-81
    The program will have the computer ask for the initial amount of principal saved by depositing in the account. Then the annual interest rate and the number of months to be displayed. The result of the run is a display of the changing principal as months pass and interest is added on.

    Program Listing

    10 LET I = 0
    20 PRINT "PRINCIPAL = $";
    30 INPUT P
    40 PRINT P
    60 INPUT R
    70 PRINT R
    90 INPUT M
    100 PRINT M
    110 FOR Q = l  TO  M
    120 LET  I= (P*(0.01*R))/12
    130 LET P = P+I
    140 PRINT Q ;" MONTH =  $ ";P
    150 NEXT Q
    160 PRINT
    170 IF INKEY$= "" THEN GOTO 170
    180 CLS
    190 GOTO 10

    Sunday, March 27, 2016 4:06 PM

All replies

  • goto statement will never disappear, as it is an essential part of machine code, in form of JZ{jump on zero) JNZ(jump on nonzero) JE (jump equal) and similar clauses.

    goto is just masked by sub...endsub and if...then..else statements


    u write in SB:

    for x=1 to 5



    and it means in assembler:

    mov  a,6 (for...)

    loop: dec a

    {statements to print...}

    JNZ loop (jump non zero)

     so it can be written also as:





    if a>0 then

    goto loop


    • Edited by Tryhest Sunday, March 27, 2016 7:04 PM
    Sunday, March 27, 2016 6:54 PM
  • sb translation of ur zx proglist:

    I = 0
    TextWindow.Write ( "PRINCIPAL = $")
    P=TextWindow.ReadNumber ()
    TextWindow.WriteLine ("")
    TextWindow.Write( "ANNUAL INTEREST RATE = ")
    R=TextWindow.ReadNumber ()
    TextWindow.WriteLine ("")
    TextWindow.Write ("NUMBER OF MONTHS = ")
    M=TextWindow.ReadNumber ()
    TextWindow.WriteLine ("")
    FOR Q = 1  TO  M
      I= (P*(0.01*R))/12
      P = P+I
      TextWindow.WriteLine ( Q +" MONTH =  $ "+P)

    Sunday, March 27, 2016 7:03 PM
  • The value of Goto is one of those Computer Science topics, like coding standards, that will usually devolve into a religious war whenever someone brings it up. Your feelings on the topic usually reflects where you started from. If you came up through academia and stayed there you probably think Goto's are evil because algorithms that use them are impossible to formulate a proof around. If you started out hacking your way through code, then Goto's are the greatest because they are quick and translate into a single machine code instruction. For most other people the topic is a great big Meh.

    Some teachers hate Goto claiming it teaches bad habits. Personally,  I love Goto. When you have students who are just starting to get the idea of programming and want to see something they have just typed in repeat forever you don't have to give a mini lecture on For or While. all you have to do is have them create a level and go to it. It's simple and it keeps them interested.

    The link below is to a famous exchange involving Linus Torvalds on the topic from the early days of the development of Linux. A very interesting read.

    Using Goto in Linux Kernal Code

    Enjoy.  :-)

    Friday, April 1, 2016 8:16 PM
  • Like @Tryhest said it'll never disappear, cause it's the essential part of the machine.

    Friday, April 1, 2016 8:18 PM
  • If i do a sample search on Bing :

    the goto statements

    return is 16,800,000 results !!!!!    so it is in fact a very popular statement in Basic !!!!

    Friday, April 1, 2016 10:34 PM
  • goto speeds up many proceses:

    • in loops there's also exitfor (not present in SB for now) which breaks for loop when condition is met. essentialy it is also jump cmd(assembler equivalent of GOTO command) 
    • in other basic versions exists also exitwhile, exitdo, exitif, break and similar cmds, all kind of goto cmd
    • there is no evil if u use a few goto cmds on every 100-200 lines of code (less than 1%)
    • avoiding the use of goto may produce too long and also too slow code sometimes

    so don't be affraid to use some goto's, but remember that less is more !

    • Edited by Tryhest Saturday, April 2, 2016 2:31 AM
    Saturday, April 2, 2016 2:29 AM
  • Here is an excellent example of why goto's are both awesome and evil:

    This is the Amazing game from "More Games in BASIC" published in the late 70's. Nearly every line ends in a goto command. This makes the code incredibly compact. In the early days of the microcomputer this was incredibly important as every byte mattered. However, on sever occasions I have attempted to analyze the code to see how it works, and I have never gotten any where. Following the logic is nearly impossible. This is what makes goto evil. Any program that relies heavily on it will be uneditable, and even the smallest changes will require you to start from scratch. 

    Monday, April 4, 2016 7:30 PM


    FORTRAN Assigned goto Statement

    We can use a label variable to modify the destination of the goto statement. Here is a FORTRAN example using an assigned goto statement:

       assign 10 to dest

       . . .

       if (hours .gt. 40.0) assign 20 to dest

       . . .

       goto dest

    Thus the program will branch to either label 10 or 20, depending on the value of label variable dest


    COBOL Alter Verb

    We now move on to the early COBOL ALTER verb that probably deserves the award for the worst programming language command. At least in the above FORTRAN and Pl/I use of variable labels, we were modifying a label variable and had some warning it may be modified. COBOL has the ALTER verb that allows us to modify a label constant. This was early on condemned and is difficult to even find in COBOL textbooks, but is still there for your use in most versions of COBOL. These undesirable commands are kept in the language to keep it upward compatible. Here is an example:




    In this code we alter the meaning of the label constant PROCESS-HEADING to point to the paragraph name (label) PROCESS-RECORDS. This could be done any place in the program and done more than once, so we have no hint on where the program is actually branching too. Much to the credit of COBOL programmers, this command was easily condemned and seldom used. The PERFORM verb is used to execute a loop, but the same problem surfaces with go to statements:


    Now the go to may branch to the PROCESS-HEADING paragraph if it has not been changed to really mean PROCESS-RECORDS. Since the ALTER verb could be part of an IF statement, exactly what is happening could be difficult to determine.

    Saturday, April 9, 2016 8:53 PM
  • this is also excellent example of so badly written code. it has many unnnecessary goto's so i presume it is adapted from another language or machine code

    the next part was a bit more easy to translate to sb:


    note that no goto is used, and in source goto is used 5 times!
    • Edited by Tryhest Sunday, April 10, 2016 5:23 AM
    Sunday, April 10, 2016 4:51 AM
  • the rest of program is kinda devil's code aka it's kind of code that it 's similar to middleages automata and mechanic clocks and it cannot be converted to modern subs bcs it becomes too long and also more and more uncomprehensible((
    Sunday, April 10, 2016 7:12 AM
  • Tryhest - an excellent example of the kind of code we had to deal with in the past and the problem with indescriminate use of GoTo. 

    I thnk you may be right about adapting from machine code - back in the day, memory and performance were much more limited so you had to eek out every bit of power and this leads to very compact but hard to read code.  Try reading MSIL, the .Net version of machine instructions, it is full of jumps.  That is why we invented more languages, especially when memory and speed increased, programs could get bigger and the importance of reusing and combining code became more important and so good structure, subroutines, classes etc are important.

    Sometimes, very high performance code is still written by hand in machine code to be super fast.  For example a simple 'If Then' actually creates a lot of instructions which can be shortened with simple jumps (GoTo).

    Sunday, April 10, 2016 11:12 AM
  • i translated it to SB but somehow it cant make as nice lab as on pic shown in revue: JSH037

    finding the error is impossible in such spaghetticode huh)))

    Sunday, April 10, 2016 9:38 PM
  • If you take a second glance at Amazing you will notice that the If-Then statements have nothing but line numbers following them. In early versions of BASIC there was no such thing as an If block. The true result of an If-Then had to be crammed onto the same line, which was limited to 256 characters. Some versions like MS-Basic featured a command separator that allowed multiple commands on that line. But most versions limited you to a single command after Then. And some versions (TI-Basic jumps to mind as as an example) didn't allow you follow a then with anything except a line number.

    The result is some amazing, but horrendous code.

    I imagine this is a side effect of the interpreters need to translate the BASIC code into assembler opcodes on the fly. The closer the BASIC was to assembler the faster this process would be. If you want to see so truly crazy code, you should look into examples to used Peek and Poke to get the job done.

    I miss those days.

    There was something magical about getting a computer to do something that wasn't supposed to be possible. These flashy GigaHertz machines we use now make everything so... easy.


    Monday, April 11, 2016 7:18 PM
  • in pascal for example, "if" is also single command but can be expanded by blocks

    Begin...end. so u write like:

    • if a>2 then
    • Begin
    •  <cmd1>
    • <cmd2>
    • end
    • else
    • Begin
    • <cmd 3>
    • <cmd4>
    • <...>
    • End;

    in SB we desperatly miss single IF ...then((

    so it could be written:

    if a>b goto c instead of

    • if a>b then
    • goto c
    • endif

    also we need IIF - "if" as a function

    so u can write stuff like: textwindow.writeline(a+iif(b>c,1, 2))

    • Edited by Tryhest Monday, April 11, 2016 7:55 PM
    Monday, April 11, 2016 7:53 PM
  • .. therefor use 'LDLogic.Switch':

    a = 1
    b = 3 ' 2
    c = 2 ' 3
    Textwindow.WriteLine(a + LDLogic.Switch(LDMath.Evaluate3(b+">"+c) ,1, 2))  '' 2 , else 3
    '' TextWindow.WriteLine(a + iif(b>c,1, 2) )

    Monday, April 11, 2016 8:50 PM
  • I for one do sincerely hope that GOTO does not go away. I have had situations where WHILE loops simply did not work so it was back to the GOTO.
    Wednesday, April 20, 2016 4:51 PM