37
4.3 COMMANDS FOR LOOPING STRUCTURES
Looping structures repeat a block of statements, either for a specified number of times or
until a certain condition is matched. In CipherLab BASIC, two kinds of looping structures,
FOR…NEXT and WHILE…WEND can be used. The command EXIT can be used as an
alternative to exit from both FOR…NEXT and WHILE…WEND loops.
Both FOR…NEXT and WHILE…WEND statements can be nested up to 10 levels.
EXIT
Purpose To provide an alternative exit for looping structures, such as FOR…NE
WHILE…WEND statements.
Syntax EXIT
Remarks EXIT can appear anywhere within the loop statement.
Example
DataCount% = TRANSACTION_COUNT
FOR Counter% = 1 TO DataCount%
Data$ = GET_TRANSACTION_DATA$(Counter%)
HostCommand$ = READ_COM$(1)
IF HostCommand$ = “STOP” THEN EXIT
WRITE_COM(1, Data$)
FOR … NEXT
Purpose
To repeat the execution of a block of statements for a specified number of
times.
Syntax FOR N% = startvalue TO endvalue [STEP step]
[Statement Block]
NEXT [N%]
Remarks “N%” is an integer variable to be used as a loop counter.
“startvalue”
is a numeric expression which is the initial value for the loop
counter.
“endvalue”
is a numeric expression which is the final value for the loop counter.
“step” is a numeric expressi
on to be used as an increment/decrement of the
loop counter The “step” is 1 by default.
If the loop counter ever reaches or beyond the endvalue, the program
execution continues to the statement following the NEXT statement. The
Statement block will be executed again otherwise.
Example
DataCount% = TRANSACTION_COUNT
FOR Counter% = 1 TO DataCount%
Data$ = GET_TRANSACTION_DATA$(Counter%)
WRITE_COM(1, Data$)