From BorielWiki
Jump to: navigation, search

Labels are identifiers where the program execution flow can jump into using either GO TO or GO SUB. Unlike variables identifiers, their scope is always global (even if declared inside functions or subroutines). Usage of labels is discouraged.

In ZX BASIC, line numbers are treated as labels:

10 REM An endless print loop
20 PRINT "Hello world!"
30 GO TO 20

Since line numbers are just labels, the order sequence is irrelevant. The following listing is equivalent to the above one. Notice the out-of-order line sequence:

10 REM An endless print loop
30 PRINT "Hello world!"
20 GO TO 30

Declaring labels

Identifiers can be used as labels. A label identifier is declared by writing it at the beginning of a line, followed by a colon:

REM Declaring a label

You can use the label with GO TO and GO SUB sentences, and with the @ operator: the previous example can be rewritten using labels instead of line numbers:

  PRINT "Hello world!"
  GO TO endlessloop

A label can also be referred before it has been declared:

  GO TO EndOfRoutine
  REM Instructions here are skipped
  PRINT "End Of Routine"