Loop structures

In cases where you want a set of sanScript statements to be executed repeatedly, you will use a loop structure. Several loop structures are available in sanScript.

For loop

The for loop iterates a variable through a specified range of variables. They are useful when you need to use the value of the variable being iterated, such as for an array index.

The following script uses a for do...end for statement to set the fields of the Monthly Total array to 0.

local integer i;

for i = 1 to 12 do
	'Monthly Total'[i] = 0;
end for;

Repeat loop

The repeat loop runs a set of sanScript statements, then tests a boolean condition to determine whether the loop should be repeated. Using a repeat...until statement ensures that the statements within the loop will be run at least once.

The following script uses a repeat loop to read through all of the records in a temporary table.

get first table Temptable;
repeat
	get next table Temptable;
until err() = EOF;

While loop

The while loop tests a boolean expression. If the expression is true, the set of sanScript statements in the loop is run. Using a while do...end while statement ensures that the condition of the loop is tested before any of the statements within the loop are ever executed.

The following example uses a while loop to read through all of the records in a table.

get first table Customer_Master;
while err() = OKAY do 
	{Not at the end of the table, so read the next item.}
	get next table Customer_Master;
end while;


Documentation Feedback