5.7. FOR..NEXT

FOR Index = Start TO End { STEP { - } Inc }

{ Body }

NEXT { Index }

The FOR..NEXT loop allows PBC programs to executes a number of statements (the Body) some number of times using a variable as an index. Due to its complexity and versatility, FOR..NEXT is best described step by step:

1)  The value of Start is assigned to the index variable, Index. Index can be a variable of any type.

2)  The Body is executed. The Body is optional and can be omitted (perhaps for a delay loop).

3) The value of Inc is added to (or subtracted from) Index. If no STEP clause is defined, Index is incremented by one.

4)  If Index is still between Start and End (inclusive), execution resumes at Step 2.

All loop calculations are performed using 16-bit arithmetic.

For B6 = 1 to 10 'Count from 1 to 10
Serout 0,N2400,(#B6," ") 'Send each number to Pin0 serially
Next B6 'Go back to top of For and do next count
Serout 0,N2400,(10) 'Send a linefeed


5.8. GOSUB


Executes the statements at Label. Unlike GOTO, when the RETURN statement is reached, execution resumes with the statement following the GOSUB statement. The code between Label and the RETURN statement is called a subroutine.

Subroutines can be nested. In other words, it is possible for a subroutine to call other subroutines. Such nesting should be restricted to no more than four levels deep.

Gosub beep 'Execute subroutine named beep
beep: High 0 'Turn on LED connected to Pin0
Sound 1,(80,10) 'Beep speaker connected to Pin1
Low 0  'Turn off LED connected to Pin0
Return 'Go back to main routine that called us


5.9. GOTO

GOTO Label

Program execution continues with the statements at Label.

Goto send 'Jump to statement labeled send
send: Serout 0,N2400,("Hi") 'Send "Hi" out Pin0 serially


5.10. HIGH


Makes the specified pin output high. The pin is automatically made an output pin. Only the pin number itself, i.e. 0 to 7, is specified (i.e. NOT Pin0.)

High 0 'Make Pin0 an output and set it high (~5 volts)