FOR - NEXT Statements

<< Click to Display Table of Contents >>

Home  mIoTa BASIC > mIoTa BASIC Language Reference > Statements > Iteration Statements (Loops) >

FOR - NEXT Statements

The FOR and NEXT statements implement an iterative loop and requires you to specify the number of iterations. The syntax of the FOR - NEXT statements is ..

 

for counter = initial_value to final_value;

 ..statements_list..

next;

 

Counter is a numerical 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 the number of loop iterations can be changed inside the loop by changing final_value.

 

With each iteration, statements_list will be executed.

 

initial_value and final_value should be expressions compatible with counter.

 

img:\\{D0C4800B-7D30-48DA-9C3C-3FE86F846921}.bmp

If final_value is a complex expression whose value can not be calculated at compile time and the 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 the result passed as a variable to the for statement's final_value. This way, the computation of the final_value expression is only done once reducing the execution time of the For - Next loop!

 

 

Here is an example of the FOR - NEXT statements ..

 

s = 0;

u1 = 5;

 

for i = 1 to u1 + 2; // i will range from 1 to 7

  s = s + a[i];

next;