do
do( ( (s_var1g_initExp1[g_stepExp1] ) (s_var2g_initExp2[g_stepExp2] ) ... ) (g_terminationExpg_terminationExp1... )g_loopExp1g_loopExp2... ) =>g_value
Description
Iteratively executes one or more expressions. Used in SKILL++ mode only.
Use do to iteratively execute one or more expressions. The do expression provides a do-while facility allowing multiple loop variables with arbitrary variable initializations and step expressions. You can declare
- One or more loop variables, specifying for each variable both its initial value and how it gets updated each time around the loop.
- A termination condition which is evaluated before the body expressions are executed.
- One or more termination expressions to be evaluated upon termination to determine a return value.
A do Expression Evaluates in Two Phases
-
Initialization phase
The initialization expressions g_initExp1, g_initExp2, ... are evaluated in an unspecified order and the results bound to the local variables var1, var2, ... -
Iteration phase
This phase is a sequence of steps, informally described as going around the loop zero or more times with the exit determined by the termination condition.
-
Each iteration begins by evaluating the termination condition.
If the termination condition evaluates to a non-nilvalue, thedoexpression exits with a return value computed as follows: -
The termination expressions terminationExp1, terminationExp2, ... are evaluated in order. The value of the last termination condition is returned as the value of the
doexpression.
Otherwise, thedoexpression continues with the next iteration as follows. - The loop body expressions g_loopExp1, g_loopExp2, ... are evaluated in order.
- The step expressions g_stepExp1, g_stepExp2, ..., if given, are evaluated in an unspecified order.
- The local variables var1, var2, ... are bound to the above results. Reiterate from step one.
Value Returned
Examples
By definition, the sum of the integers 1, ..., N is the Nth triangular number. The following example finds the first triangular number greater than a given limit.
procedure( trTriangularNumber( limit )
do(
( ;;; start loop variables
( i 0 i+1 )
( sum 0 ) ;;; no step expression
;;; same as ( sum 0 sum )
) ;;; end loop variables
( sum > limit ;;; test
sum ;;; return result
)
sum = sum+i ;;; body
) ; do
) ; procedure
trTriangularNumber( 4 ) => 6
trTriangularNumber( 5 )
=> 6
trTriangularNumber( 6 )
=> 10
Related Topics
Return to top