Mikroelektronika MIKROE-350 Fiche De Données

Page de 526
FOR STATEMENT
The for statement implements an iterative loop and requires you to specify the num-
ber of iterations. The syntax of the for statement is:
for counter = initial_value to final_value [step step_value]
statements
next counter
counter
is a variable being increased by 
step_value
with each iteration of the loop.
The parameter 
step_value
is an optional integral value, and defaults to 1 if omit-
ted. Before the first iteration, 
counter
is set to 
initial_value
and will be incre-
mented until it reaches (or exceeds) the 
final_value
. With each iteration, state-
ments will be executed.
initial_value
and 
final_value
should be expressions compatible with 
counter
;
statements
can be any statements that do not change the value of 
counter
.
Note that the parameter 
step_value
may be negative, allowing you to create a
countdown.
Here is an example of calculating scalar product of two vectors, 
a
and 
b
, of length
n,
using the for statement:
s = 0
for i = 0 to n-1
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.
156
MIKROELEKTRONIKA
- SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
Language Reference
mikroBasic PRO for AVR
CHAPTER 5