DO statement
Groups statements into a single block, optionally specifying processing services or block properties. Use an END statement to end a DO block.
Syntax
FORrecord
[ ,record
] ...
Names the buffer you want to work with in the block and scopes the buffer to the block. The scope of a record determines when the buffer for that record is cleared and written back to the database. See OpenEdge Getting Started: ABL Essentials for more information on record scoping.To work with a record in a table defined for multiple databases, you must qualify the record’s table name with the database name. See the Record phrase reference entry for more information.preselect-phrase
The PRESELECT phrase finds selected records from one or more tables. You can access those preselected records with statements such as FIND NEXT.
For more information, see the PRESELECT phrase reference entry.query-tuning-phrase
variable
=expression1
TOexpression2
[ BYk
]
The name of a field or variable whose value is incremented in a loop. Theexpression1
is the starting value forvariable
on the first iteration of the loop. Thek
is the amount to add tovariable
after each iteration, and it must be a constant. Thek
defaults to 1. Thevariable, expression1
andexpression2
must be integers.Whenvariable
exceedsexpression2
(or is less thanexpression2
ifk
is negative) the loop ends. Sinceexpression1
is compared toexpression2
at the start of the first iteration of the block, the block can be executed zero times. Theexpression2
is re-evaluated on each iteration of the block.WHILEexpression
Indicates that the DO block continues processing the statements within it. Using the WHILE option turns a DO block into an iterating block. The block iterates as long as the condition specified by the expression is TRUE. The expression is any combination of constants, operators, field names, and variable names that yield a logical value.TRANSACTIONSTOP-AFTERexpression
The STOP-AFTER phrase specifies a time-out value for a DO, FOR, or REPEAT block. The integer expression specifies the number of seconds each iteration of a block has until a time-out occurs. If a time-out occurs, the AVM raises the STOP condition and default STOP condition handling occurs. Use an ON STOP phrase on the block (or an enclosing block) to alter the default STOP condition handling.If the block iteration completes before the specified time expires, the timer resets toexpression
for the next iteration. In other words, the timer is limited to the scope of a single block iteration. If a block with a STOP-AFTER phrase encloses another block or calls another block, the timer continues while the inner blocks execute.If a block with a STOP-AFTER phrase contains a nested block with a STOP-AFTER phrase, then each has a timer in effect. If the outer block timer expires while the inner block is executing, the STOP condition is raised even if the timer for the inner block has not expired.If the STOP condition is handled and execution resumes within the scope of a block with a STOP-AFTER phrase, no timer is in effect until the next iteration of a block with a STOP-AFTER phrase. In other words, all old timers are dismissed but new timers can now be established.When the timer expires, the STOP condition is raised on the current statement.Two important use cases for the STOP-AFTER phrase are to time-limit dynamic queries and to time-limit a procedure call. The following example time-limits a procedure call using a RUN statement:
Use this technique to also make timed calls to class methods and user-defined functions.The following example is simplified code that lets you try different STOP-AFTER cases.
If you run this code as is, the outer DO block establishes a 5 second time limit for the work of the DO block and all inner blocks. When the inner FOR EACH block starts, another timer is established for the first iteration of this block. When the first FOR EACH iteration completes, its timer is reset to 1 second for the next iteration. Meanwhile, the outer timer on the DO block continues without interruption.The FOR EACH block completes and execution continues forward to the REPEAT block, which is an endless loop. The REPEAT block also has a 1 second timer for each iteration of the block. At some point, the outer 5 second timer elapses and the AVM raises the STOP condition. The STOP condition is raised on the statement the AVM was executing when the timer elapsed. Normal STOP handling proceeds from that point.As the stack unwinds during STOP processing, the AVM encounters the ON STOP phrase on the DO block. The ON STOP phrase dismisses the STOP condition and resumes normal execution with the next statement following the DO block, as directed by the LEAVE option.If you remove the comments from the IF statement in the REPEAT block, the block will complete within the outer time limit and the STOP condition is not raised.If you want to experiment with elapsed timers on an inner block, insert a complex operation inside the FOR EACH block.In the following example, the STOP-AFTER expression is modified during program execution:
Because theSTOP-AFTER
expression is re-evaluated for each iteration of a looping block, any changes made to the expression during the iteration effect the timer for the block. In the example, theSTOP-AFTER
time limit is specified by the variablestopTime
, which is initially set to 30 seconds. The procedure contains an iterating block which runs a procedure that executes for 10 seconds.On the first iteration of theDO WHILE TRUE
loop,stopTime
is 30 seconds. The loop executes for 10 seconds, and then dividesstopTime
by 2. On the second iteration, thestopTime
is 15 seconds; again the loop executes for 10 seconds, and then dividesstopTime
by 2. On the third iteration, thestopTime
is 8 seconds. This time, the procedurespinHere
runs for 8 seconds and then raisesSTOP
. TheSTOP
condition is handled by theDO
block, and then the program displays the message program finished.If a code block is called with a time limit of zero, the block is executed as if theSTOP-AFTER
phrase was omitted from the block declaration.Consider the following example:
In this example, procedurefoo
is run from within a timed block with a 10 second time limit; procedure bar is called from within the timed block, and contains an iterating block that specifies theSTOP-AFTER
phrase. Because the value of theSTOP-AFTER
expression evaluates to zero (that is, the current value of thebarLimit
variable), the block withinbar
is executed as an untimed block. However, the rules for execution of an untimed block within a timed block apply, so the untimed block in bar is executed with an implicit iteration time limit of 10 seconds.Other points to consider are:
- If the expression evaluates to zero or less, then this is the equivalent of not specifying a STOP-AFTER phrase.
- STOP-AFTER phrases are not intended to interact with user interfaces.
- Blocking calls to third party software components, where the AVM has transferred execution control, cannot be timed out. This category includes operating system calls, MS Windows system calls, and calls to any third party DLLs and Unix shared objects.
on-endkey-phrase
Describes the processing that takes place when the ENDKEY condition occurs during a block. This is the syntax for the ON ENDKEY phrase:
For more information, see the ON ENDKEY phrase reference entry.on-error-phrase
Describes the processing that takes place when there is an error during a block. This is the syntax for ON ERROR phrase:
For more information, see the ON ERROR phrase reference entry.on-quit-phrase
Describes the processing that takes place when a QUIT statement is executed during a block. This is the syntax for ON QUIT phrase:
For more information, see the ON QUIT phrase reference entry.on-stop-phrase
Describes the processing that takes place when the STOP conditions occurs during a block. This is the syntax for the ON STOP phrase:
For more information, see the ON STOP phrase reference entry.frame-phrase
Specifies the overall layout and processing properties of a frame. For more information onframe-phrase
, see the Frame phrase reference entry.do-body
do-logic
catch-block
Specifies a CATCH statement that defines error handling code for one or more error types. Since a DO block does not have any default error handling, a CATCH block is only valid within it if the block specifies error handling options. At this point, the block can raise ERROR and a CATCH block is valid to handle errors. If a DO block does not have error handling options and specifies a CATCH block, ABL generates a compiler warning. For more information oncatch-block
, see the CATCH statement reference entry.finally-block
Specifies a FINALLY statement that defines the processing that must occur after all other processing in the block occurs. For more information onfinally-block
, see the FINALLY statement reference entry.END
Specifies the end of the DO block definition. You must end the DO block definition with the END statement.ExampleThis procedure goes through the Customer table and, for those Customers whose CreditLimit is over 80000, reduces CreditLimit to 80000. The procedure uses an unmodified DO block to process two statements if CreditLimit is over 80000. Unmodified DO blocks are most useful in conditional, or IF. . .THEN. . .ELSE situations.
Notes
- Use a DO statement rather than a REPEAT statement when you loop through each element of an array. This way the AVM does not create separate subtransactions within a transaction.
For example, the first transaction is more efficient then the second:
- For SpeedScript, the
on-endkey-phrase
and theon-quit-phrase
do not apply.See alsoCATCH statement, FINALLY statement, FIND statement, FOR statement, Frame phrase, ON ENDKEY phrase, ON ERROR phrase, ON QUIT phrase, ON STOP phrase, Record phrase, REPEAT statement
OpenEdge Release 10.2B
|