beautypg.com

Rpbasic-52 programming guide, For-to-step-next – Remote Processing BASIC 52 User Manual

Page 57

background image

RPBASIC-52 PROGRAMMING GUIDE

2-38

FOR-TO-STEP-NEXT

Syntax:

F O R variable =initial index expr T O index limit expr [STEP step expr ]
progra m statem ents
NEXT [variable ]
Where: variable = any valid variable symbol

initial index expr = starting value assigned to variable
index limit expr
= ending va lue of variable
step expr
= optional increment or decrement to variable when repeating a loop

Function:

Looping structure useful for executing a sequence of instructions a numb er of times.

Mode:

Run, command

Use:

F O R A = 0 to 4 0 00 S T E P 20 0 : A O T 0 ,A : N E X T

Cards:

All

D E S C R IP T I ON

The FO R-TO -STEP -NEX T instruction is a loop structure com mon to m any high leve l languages. It is used to
p e rf or m progra m statem ents a num ber of tim es.

variable is a loop counter initialized to initial index expr at the start of the loop. A n umber of program
statemen ts
are executed until NEX T is encoun tered. At this point the v alue of step expr is added to the value
of variable . T he r es u lt i ng n e w variable value is com pared to the valu e of index limit expr. If the new value
of variable value is less than or e qual to the value of index limit expr, all progra m statem ents are executed
again , and the test is re peate d.

progra m statem ents are always e xecuted at lea st once. If step expr is larger than index limit expr, the loop
executes only once.

STEP is optional. Wh en omitted, it defa ults to 1. The value of step expr may be positive or negative.

FOR -NE XT l oops m ay be i nside o ther FO R-N EXT loops. variable following N EXT is optional.

There are tw o ways to bre ak out of a for next loo p and still main tain the control stack . The first is to execute
a C L E A R S co m m a n d. T hi s co m m a n d a l so c le a rs a n y s u br o ut i ne r et u rn l oc a ti o ns a n d D O - W H I L E , D O -
UNT IL loops. Ano ther is to set variable to a high value within program statements. When a program
continuously bre aks out of a FO R-NE XT loop and re-declares a new loop, a C -Stack error is even tually
returned.

RELATED

DO-UNTIL, DO-WHILE

E R R O R

C - S T A C K

N E X T w i th o u t a c o rr e sp o n di n g F O R . T hi s er r or c an a ls o a pp e a r i f a n u m b er o f F O R - N E X T
loops were set up but were illegally branched out of or re-declared.