Mikroelektronika MIKROE-724 データシート
mikroBasic PRO for dsPIC30/33 and PIC24
MikroElektronika
229
For Statement
The
for
statement implements an iterative loop and requires you to specify the number of iterations. The syntax of the
for
statement is:
for counter = initial_value to final_value [step step_value]
statement_list
next counter
counter
is a variable which increments with each iteration of the loop. Before the first iteration,
counter
is set to
initial_value
and will increment until it reaches
final_value
.
final_value
will be recalculated each time the
loop is reentered.
This way number of loop iterations can be changed inside the loop by changing
final_value
. With each iteration,
statement_list
will be executed.
initial_value
and
final_value
should be expressions compatible with
counter
;
statement_list
may be
consisted of statements that don’t change the value of the
counter
.
Note that the parameter
step_value
may be negative, allowing you to create a countdown.
If
final_value
is a complex expression whose value can not be calculated in compile time and number of loop
iterations is not to be changed inside the loop by the means of
final_value
, it should be calculated outside the for
statement and result should be passed as for statement’s
final_value
.
statement_list
is a list of statements that
do not change the value of counter.
Here is an example of calculating scalar product of two vectors,
a
and
b
, of length
10
, using the
for
statement:
s = 0
for i = 0 to 9
s = s + a[i] * b[i]
next i
Endless Loop
The
for
statement results in an endless loop if
final_value
equals or exceeds the range of the
counter
’s type.
While Statement
Use the
while
keyword to conditionally iterate a statement. The syntax of the
while
statement is:
while expression
statements
wend
statements
are executed repeatedly as long as
expression
evaluates true. The test takes place before
statements
are executed. Thus, if
expression
evaluates false on the first pass, the loop does not execute.