There is a third kind of iterating block that is in between DO and FOR in its effects, the REPEAT block. It supports just about all the same syntax as a FOR block. You can add a FOR clause for one or more tables to it. You can use a WHILE phrase or the expression TO expression phrase. You can scope a frame to it.

A block that begins with the REPEAT keyword shares these default characteristics with a FOR block:

  • It is an iterating block
  • It scopes a frame to the block
  • It scopes records referenced in the REPEAT statement to the block
  • It provides transaction processing if you update records within the block

By contrast, it shares this important property with a DO block: it does not automatically read records as it iterates.

So what is a REPEAT block for? It is useful in cases where you need to process a set of records within a block but you need to navigate through the records yourself, rather than simply proceeding to the next record automatically on each iteration. The sample procedure in Index cursors shows you an example of where to use the REPEAT block.

One of the common ways to use a REPEAT block in older character applications is to repeat a data entry action until the user hits the ESCAPE key to end. Here is a very simple but powerful example:

REPEAT:
  INSERT Customer EXCEPT Customer.Comments WITH 2 COLUMNS.
END.

You have not seen the INSERT statement before and you will not see much of it again, even though it is one of the most powerful statements in the language. The following figure shows what you get from that one simple statement.

Figure 1. Results of using the INSERT statement

It's a complete data entry screen, complete with initial values for fields that have one. The field help displays at the bottom of the window. Inside a REPEAT loop, this lets you create one new Customer record after another until you're done and you press ESCAPE.

Why won't you see the INSERT statement again? Because INSERT is one of those statements that mixes up all aspects of ABL, from the user interface to saving the data off into the database. And in a modern GUI application, you need to separate out all those things into separate parts of your application.