Liberty BASIC Help Online

Branch Labels, GOTO and GOSUB
See also:   GOSUB, RETURN, GOTO, Functions and Subroutines, Function, Sub
 
Branch Labels
Liberty BASIC will accept numbers as branch labels.  This is useful when using old BASIC code.  The numbers don't change the order of the code.  The following two short programs are essentially the same:
 
10 REM count to ten
20 for x = 1 to 10
30 print x
40 next x
50 end
 
20 REM count to ten
40 for x = 1 to 10
10 print x
30 next x
50 end
 
Instead of using numeric branch labels, it is better to use alphanumeric ones, because they are descriptive and help the programmer remember what is happening in a block of code.  In Liberty BASIC, alphanumeric branch labels are surrounded by square braces:  [myBranchLabel]  Spaces and numbers are not allowed as part of branch label names, and names may not start with a numeral.
 
Here are some valid branch labels:  [mainMenu]  [enterLimits]  [repeatHere]
Here are some invalid branch labels:  [enter limits]  mainMenu  [1moreTime]
 
In QBASIC, branch labels end with a colon and looks like this:  myBranchLabel:
 
The above program doesn't really need branch labels.  It looks like this without them:
 
  'count to ten
  for x = 1 to 10
    print x
  next x
  end
 
A program to count without a for/next loop looks like this:
 
  'count to ten
[startLoop]
  x = x + 1
  print x
  if x < 10 then [startLoop]
  end
 
Just for comparison, in QBASIC it looks like this:
 
  'count to ten
startLoop:
  x = x + 1
  print x
  if x < 10 then goto startLoop
  end
 
NOTE: Branch labels inside functions and subroutines are not visible to code outside those functions and subroutines.  Likewise, functions and subroutines cannot use branch labels defined outside their scope.
 
GOTO
To continue program execution at a specified branch lable, issue a GOTO command.  See GOTO.
 
    GOTO [startLoop]
 
Do not use GOTO to exit a FOR/NEXT or WHILE/WEND loop prematurely.
 
GOSUB
To continue program execution at a specifed GOSUB routine, issue a GOSUB command.  The GOSUB routine ends with a RETURN statement that returns program execution to the line of code following the GOSUB command.  See GOSUB, RETURN.
 
GOSUB [initialize]


Copyright (C) 2003 Shoptalk Systems
Liberty BASIC - http://www.libertybasic.com/