FANDOM


Looping Commands Intro Edit

This chapter includes the "normal" BASIC commands that are included with most versions of BASIC, as well as commands specific to Atom BASIC. Read it carefully: some familiar commands may be defined somewhat differently in Atom BASIC.

This chapter contains the following sections:

  • Looping Commands
    • For...next
    • Do...while
    • While...wend
    • Repeat...until


Conventions Used in this Chapter Edit

{ ... } represent optional components in a command. The { } are not to be included.
[ ... ] used for lists - the [ ] are required.
( ... ) used for some arguments. The ( ) are required.


Looping Commands Overview Edit

Looping commands repeat a number of lines (instructions) multiple times, depending on certain conditions.

Command Repeats Condition tested
for...next defined number of times at beginning of loop
do...while until false at end of loop
while...wend until false at beginning of loop
repeat...until until true at end of loop


FOR...NEXT Edit

Repeats the instructions between FOR and NEXT a predefined number of times.

Syntax

for counter = startvalue to endvalue {step stepvalue}
statements to be executed
next
counter is a variable used to hold the current counter value
startvalue is the initial value of the loop counter
endvalue is the final value of the loop counter
stepvalue is the optional increment or decrement

These values may be bit, nibble, byte, word, long or float. Startvalue, endvalue and stepvalue may be variables or constants.

If STEP is omitted a stepvalue of 1 is automatically assigned.

Stepvalue may be negative in which case the counter will be decremented rather than incremented. The loop will continue until the counter value falls outside the range set by endvalue.

Note: Unlike some BASICs, "next" does not have an argument in Atom BASIC, i.e. the form "next x" is not valid.

Important: Take care not to modify the value of counter using statements within the loop. This can cause unpredictable operation, and the loop may never end.

Examples

ant var byte
bat var byte(11)
for ant = 1 to 10
bat(ant) = ant * 20
next

This simple loop will store values in the array variable "bat" as follows:

bat(0) = unchanged, bat(1) = 20, bat(2) = 40... bat(10) = 200
a var word
for a = 10 to 20 step 5
{statements}
next

The statements will be executed 3 times with a = 10, a = 15 and a = 20. The value of "a" is incremented and tested at the end of the loop.

a var word
for a = 10 to 20 step 6
{statements}
next

The statements will be executed twice with a = 10 and a = 16.

a var sword
for a = 40 to 20 step -5
{statements}
next

The statements will be executed 5 times with a = 40, 35, 30, 25 and 20 respectively.


DO... WHILE Edit

Important: You must not nest a WHILE... WEND inside a DO... WHILE. A compiler error will result.

Repeats a set of instructions as long as a given condition remains true (i.e. until the given condition becomes false).

The condition is tested after the instructions have been executed. The instructions will be executed once even if the condition is initially false (see the second example below).

Syntax

do
statements
while condition
condition is any valid combination of variables, constants and logical operators.

Examples

a var word
a = 5
do
   a = a * 2
   hserout [dec a]
while a < 100
statements

The loop operates as follows:

Pass Output (a) Test result
1 10 true
2 20 true
3 40 true
4 80 true
5 160 false

Since the test is done at the end of the loop, the final value is output even though it is greater than 100. Program execution continues with the line following "while".

a var word
a = 150
do
   a = a * 2
   hserout [dec a]
while a < 100
statements

The loop will operate once, and output the value 300, even though the initial value is not less than 100. This is because the test is done at the end of the loop.


WHILE... WEND Edit

Important: You must not nest a WHILE... WEND inside a DO... WHILE. A compiler error will result.

Repeats a set of instructions as long as a given condition remains true (i.e. until the given condition becomes false).

The condition is tested before the instructions are been executed. If the condition is initially false, the instructions will never be executed.

Syntax

while condition
program statements
wend
condition is any valid combination of variables, constants and logical operators.

Examples

a var word
a = 5
while a < 100
   a = a * 2
   hserout [dec a]
wend
program continues

The loop operates as follows:

Pass Initial (a) Test result Output
1 5 true 10
2 10 true 20
3 20 true 40
4 40 true 80
5 80 true 160
6 160 false none

On pass number 6 the test is false so the loop is not executed. Program execution continues with the line following WEND. The results are similar to the DO... WHILE loop shown above.

The following example illustrates a difference between the DO... WHILE and WHILE... WEND loops.

a var word
a = 150
while a < 100
   a = a * 2
   hserout [dec a]
wend
program continues

Unlike the DO... WHILE loop, the WHILE... WEND tests before the loop statements are executed. Since the condition is false initially, the loop is never executed and control passes to the statements following WEND. (Contrast this with the DO... WHILE loop which executes once in a similar situation.)


REPEAT... UNTIL Edit

Repeats a set of instructions until a given condition becomes true (i.e. as long as the condition remains false).

The condition is tested after the instructions have been executed. The instructions will be executed once even if the condition is initially true. REPEAT... UNTIL is essentially the converse of DO... WHILE.

Syntax

repeat
program statements
until condition
condition is any valid combination of variables, constants and logical operators.

Examples

a var word
a = 5
repeat
   a = a * 2
   hserout [dec a]
until a > 100
program continues

The loop operates as follows:

Pass Output (a) Test result
1 10 false
2 20 false
3 40 false
4 80 false
5 160 true

Program execution then continues with the line following UNTIL.

If the initial value of a is greater than 100, the loop will be executed once because the test is at the end of the loop.

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.