Liberty BASIC Help Online

Looping
Liberty BASIC provides three constructions for looping.  One is FOR/NEXT another is WHILE/WEND, and the third is DO LOOP.
 
FOR/NEXT executes a loop a set number of times, which is determined by the starting and ending values in the FOR statement and by the size of the STEP.  For more on For...Next loops, click here.
 
WHILE/WEND executes a loop as long as a specified condition evaluates to TRUE.  For more on While...Wend loops, click here.
 
DO LOOP provides a loop that always executes once and then only loops back as long as a condition is met. For more on DO LOOPs, click here.
 
WARNING: In loops, an "exit" statement is provided for instances where it is necessary to exit the loop before the counter variable has reached its max, or the "while" condition evaluates to false.  Do not attempt to exit a loop prematurely by issuing a "goto" statement.
 
FOR/NEXT
The FOR . . . NEXT looping construct provides a way to execute code a specific amount of times. A starting and ending value are specified:
 
    for var = 1 to 10
      {BASIC code}
    next var
 
In this case, the {BASIC code} is executed 10 times, with var being 1 the first time, 2 the second, and on through 10 the tenth time.  Optionally (and usually) var is used in some calculation(s) in the {BASIC code}. For example if the {BASIC code} is  print var ^ 2, then a list of squares for var will be displayed upon execution.
 
The specified range could just as easily be 2 TO 20, instead of 1 TO 10, but since the loop always counts +1 at a time, the first number must be less than the second.  The way around this limitation is to place STEP n at the end of for FOR statement:
 
    for index = 20 to 2 step -1
      {BASIC code}
    next index
 
This loops 19 times returning values for index that start with 20 and end with 2.  STEP can be used with both positive and and negative numbers and it is not limited to integer values.  For example:
 
    for x = 0 to 1 step .01
      print "The sine of "; x; " is "; sin(x)
    next x
 
For more on For...Next loops, click here.
 
WHILE/WEND
As shown above, Liberty BASIC includes a for/next looping construct.  It also has a while/wend looping construct.  Here is the above program rewritten using while/wend.
 
  'count to ten
  x = 0
  while x < 10
    x = x + 1
  print x
  wend
  end
 
One useful thing that can be done with while/wend is to wrap the boolean expression in a NOT() function, which effectively turns while/wend into a "while condition is false" do construct:
 
  'count to ten
  while not(x = 10)
    x = x + 1
  print x
  wend
  end
 
For more on While...Wend loops, click here.
 
DO LOOP
Liberty BASIC also provides a DO LOOP structure for cases when you want a loop that always executes once and then only loops back as long as a condition is met. It will continue looping back and executing the code as long as the booleanExpr evaluates to true. Here is the routine that counts to 10 using DO LOOP with while and DO LOOP with until.
 
'count to 10 using "loop while" and "loop until"
    do
        print a
        a = a + 1
    loop while a < 11
    print
 
    do
        print b
        b = b + 1
    loop until b = 11
 
For more on DO LOOPs, click here.
 


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