User manual
227
mikoPascal PRO for dsPIC30/33 and PIC24
MikroElektronika
Iteration Statements
Iteration statements let you loop a set of statements. There are three forms of iteration statements in mikroPascal PRO
for dsPIC30/33 and PIC24:
- for
- while...do
- do
You can use the statements break and continue to control the ow of a loop statement. break terminates the statement
in which it occurs, while continue begins executing the next iteration of the sequence.
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 nal_value do statement_list
// or
for counter := initial_value downto nal_value do statement_list
counter is a variable which increments (or decrements if you use downto) with each iteration of the loop. Before
the rst iteration, counter is set to initial_value and will increment (or decrement) until it reaches nal_value.
nal_value will be recalculated each time the loop is reentered.
This way number of loop iterations can be changed inside the loop by changing nal_value. With each iteration,
statement_list will be executed.
initial_value and nal_value should be expressions compatible with counter.
If nal_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 nal_value, it should be calculated outside the for
statement and result should be passed as for statement’s nal_value. statement_list is a list of statements
that do not change the value of counter. If statement_list contains more than one statement, statements must be
enclosed within begin-end block.
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 do
s := s + a[i] * b[i];
Endless Loop
The for statement results in an endless loop if nal_value equals or exceeds the range of the counter’s type.
More legible way to create an endless loop in Pascal is to use the statement while TRUE do.