In This Topic
Loop Structures
The two different types of loop structures available are: those that only loop a set number of times, and those that loop continuously until a certain condition is met (i.e. an expression evaluates to TRUE). When using the latter type, make sure that it is possible for the terminating condition to be met, otherwise an infinite loop will occur.
Loops can also be categorised into pre-test or post-test loops, depending on whether the condition is tested before the loop runs or after. The difference is that a post-test loop must run at least once.
WHILE WEND
Repeatedly runs a set of instructions while a condition is true (pre-test).
Syntax:
WHILE loop_condition stat_list WEND
WHILE ( loop_condition ) { stat_list }
WHILE ( loop_condition ) DO BEGIN stat_list END
- Loop_condition is an expression that evaluates to either TRUE or FALSE.
- Stat_list is a group of statements (or single statement) that is executed each time the loop runs.
DO WHILE / UNTIL
Repeatedly runs a set of instructions while a condition is true, or until the condition is false (pre-test).
Syntax:
DO WHILE loop_condition stat_list LOOP
DO UNTIL loop_condition stat_list LOOP
- Loop_condition is an expression that evaluates to either TRUE or FALSE.
- Stat_list is a group of statements (or single statement) that is executed each time the loop runs.
DO LOOP WHILE / UNTIL
Repeatedly runs a set of instructions while a condition is true, or until the condition is false (post-test).
Syntax:
DO stat_list LOOP WHILE loop_condition
DO stat_list LOOP UNTIL loop_condition
DO { stat_list } WHILE ( loop_condition )
DO BEGIN stat_list END [LOOP] WHILE ( loop_condition )
REPEAT stat_list UNTIL loop_condition
- Loop_condition is an expression that evaluates to either TRUE or FALSE
- Stat_list is a group of statements (or single statement) that is executed each time the loop runs.
FOR
Runs a set of instructions, first evaluating the start expressions, looping while the condition is true, and executing the iteration expression each time through
Syntax:
FOR ( [ start ] ; [ condition ] ; [iteration] ) statement
FOR ( [ start ] ; [ condition ] ; [iteration] ) { stat_list }
- Start is the expression to execute before the loop starts.
- Condition is the condition to test at the start of each loop, exit the loop when it is false.
- Iteration is an expression that is evaluated at the end of each loop.
- Statement is a statement that is executed each time the loop runs.
- Stat_list is a group of statements that is executed each time the loop runs.
FOR NEXT
Runs a set of instructions, stepping a variable each time, until an ending value is reached
Syntax:
FOR count [ = | := ] start_count [ TO | DOWNTO ]end_count [ STEP step_amount ] stat_list [ NEXT [ count ] | END FOR ]
FOR count [ = | := ] start_count [ TO | DOWNTO ]end_count [ STEP step_amount ] { stat_list }
FOR count [ = | := ] start_count [ TO | DOWNTO ]end_count [ DO ] BEGIN stat_list END
- Count is a variable name in which the number of the current loop is stored.
- Start_count is the initial value of the count variable.
- End_count is the value that, when equal to the count variable, ends the loop.
- Step_amount is how much to increase the count variable by each time the loop is run.
- Stat_list is a group of statements (or single statement) that is executed each time the loop runs.
FOR EACH NEXT
Runs a set of instructions one for each value in an
arraySyntax:
FOR EACH id IN array statlist NEXT id
FOR EACH id IN array statlist END FOR
FOR EACH id IN array { statlist }
FOR EACH id IN array [ DO ] BEGIN statlist END
- Id is a variable name which will get each value of the array in turn.
- Array is the array whose values we will iterate.
- Stat_list is a group of statements (or single statement) that is executed for each value in the array.
Runs a set of instructions one for each [key,value] in a map
Syntax:
FOR EACH key,value IN map statlist NEXT id
FOR EACH key,value IN map statlist END FOR
FOR EACH key,value IN map { statlist }
FOR EACH key,value IN map [ DO ] BEGIN statlist END
- Key is the index string which will get each key of the map in turn.
- Value gets the value for this key as we get each [key,value] entry of the map in turn.
- Map is the map whose [key,value] entries we will iterate through.
- Stat_list is a group of statements (or single statement) that is executed for each [key,value] in the map.