Home > CBM > BASIC > Keywords > Goto
180 Subpages: (divide), (equal), (less), (minus), (more), (multiply), (plus), (power), Abs, And, Append, Asc, Atn, Auto, Backup, Bank, Begin, Bend, Bload, Boot, Box, Bsave, Bump, Catalog, Char, Chr, Circle, Close, Clr, Cmd, Collect, Collision, Color, Concat, Cont, Copy, Cos, Data, Dclear, Dclose, Dec, Def, Delete, Dim, Directory, Dispose, Dload, Do, Dopen, Draw, Ds, Ds string, Dsave, Dverify, El, Else, End, Envelope, Er, Err, Exit, Exp, Fast, Fetch, Filter, Fn, For, Fre, Get, Get num, Getkey, Go, Gosub, Graphic, Gshape, Header, Help, Hex, If, Input, Input num, Instr, Int, Joy, Key, Left, Len, Let, List, Load, Locate, Log, Loop, Mid, Monitor, Movspr, New, Next, Not, Off, On, Open, Or, Paint, Peek, Pen, Pi, Play, Pointer, Poke, Pos, Pot, Print, Print num, Pudef, Quit, Rclr, Rdot, Read, Record, Rem, Rename, Renumber, Restore, Resume, Return, Rgr, Right, Rlum, Rnd, Rreg, Rspcolor, Rsppos, Rsprite, Run, Rwindow, Save, Scale, Scnclr, Scratch, Sgn, Sin, Sleep, Slow, Sound, Spc, Sprcolor, Sprdef, Sprite, Sprsav, Sqr, Sshape, St, Stash, Step, Stop, Str, Swap, Sys, Tab, Tan, Tempo, Then, Ti, Ti string, To, Trap, Troff, Tron, Until, Using, Usr, Val, Verify, Vol, Wait, While, Width, Window, Xor

KeywordAbbreviationToken (hex)Version(s)Classification
GOTOG{Shift+O}891.0 to 7.0Command and Statement
GO TOnone CB 20* A4 2.0 to 7.0Command and Statement 
* zero or more spaces (hex 20) may appear between CB and A4

 
Syntax  
GOTO lineNumber [ junk ]
~ or ~
ON selector  GOTO lineNumber [ , lineNumber ] ...

ParametersTypeLegal Value(s)Default ValueNote(s)
selectorUnsigned Byte0 ~ 255
lineNumberUnsigned Integer0 ~ 63999If choosen, it must exist.  Must be a literal number.
junkLiteral charactersany except colon (:)Not begin with a digit; ignored like DATA

 
Purpose  
Program flow control.  Transfer program execution to the start of a program line.

 
Remarks  
The original GOTO (without a space) and the newer GO TO (with one or more spaces) are suppose to be indentical / synomyous.  However, a SYNTAX ERROR will be generated if the ON preposition is used with the (newer) GO TO version.  Fail!  Or as I like to say: if it ain't broke, don't fix it.

GOTO can be used for unconditional program flow re-direction.  However it is typically used in conjunction with a decision making process.  The most typical case being the IF/THEN/ELSE construct.  Since some BASIC versions lack ELSE, excpect them to have more instances of pure GOTOs.

Although the main (trailing) argument to GOTO, the lineNumber, is simply a number, CBM BASIC does not allow the number to be contained in a variable or calculated from an expression.  Why, why, why?  It seems to make a RENUMBER routine more complex, but the original version(s) of CBM BASIC did not have RENUMBER so the answer must be something else...  You might need to ask Bill Gates!

Anyway, a kludgy method around the "no variables, no expressions" restriction is the use of the ON preposition.  When this is present, the selector is evaluated.  If the result is less than 0 (or greater than 255), then an ILLEGAL QUANTITY ERROR is generated.  Otherwise the selector's value (call it N) is used to select the Nth lineNumber in the list.  Not all N possibilities need to be entered.  If N is greater than the number of lineNumbers following GOTO, then the GOTO is completely ignored and the statment following it (whether on the same line or the next line) will be executed instead.  If N is less/equal to the count of lineNumbers listed, then Nth one is used.  Not all N values need to have a corresponding lineNumber, but for any N that is actually choosen, its corresponding line number must exist in the program or an UNDEF'D STATEMENT ERROR occurs.  If the Nth lineNumber is "empty" (no number) and is selected, then instead a SYNTAX ERROR message will be generated (all other cases work as described above).  If the Nth lineNumber is junk then everything will run fine when N is > 0 and N < junk entry, but when N = 0 or N >= to the junk entry, you will also get SYNTAX ERROR.

Sorry if that seems complex!  Now you know why I call it a kludge.  Some examples are below.  Also it should be noted that ON/GOTO is very similar (and more compact) than the SELECT/CASE of VisualBASIC or SWITCH/CASE of C.  Anyway, it is cleaner and faster than a long chain of IF/THEN/ELSE statements, which would otherwise be needed.

If GOTO is executed in direct mode, the BASIC interpreter will change its internal state from from "direct mode" to "run mode".  While the concept may seem obvious, the details are not well-documented!  See this topic for details.   When this happens, BASIC updates a secret variable (sets it to 128).  It is secret because BASIC provides no way to read the value (except for machine-specific PEEK).


Example 1:
10 GOTO 10 : REM infinite loop, do not RUN!
NEW

READY.
10 INPUT "N"; N
20 ON N GOTO 100, , 300, 400
30 PRINT "INVALID" : GOTO 10
100 PRINT "ONE" : GOTO 10
300 PRINT "THREE" : GOTO 10
RUN
N? 3 (user input)
THREE
N? 5 (user input)
INVALID
N? 0 (user input)
INVALID
N? -1 (user input)
?ILLGAL QUANTITY ERROR IN 20

READY.
RUN
N? 1 (user input)
ONE
N? 2 (user input)
?SYNTAX ERROR IN 20

READY.
RUN
N? 3 (user input)
THREE
N? 4 (user input)
?UNDEF'D STATEMENT ERROR IN 20

READY.

Example 2 with junk:
10 GOTO 30 SOMETHING
30 GOTO 40-10 :REM -10 is ignored so go to 40
40 PRINT "HERE I AM"
RUN
HERE I AM

READY.

  Compare With  
CHAR, COLLISION, CONT, GOSUB, LOCATE, MOVSPR,  RECORD, RESTORE, RUN, TRAP

  Contrast With  
END, STOP

  See Also  
DATA, DO, EL, FOR, GO, IF, ON, TO


© H2Obsession, 2014