IBM Books

Administration and Programming Guide for OS/400

Looping Constructs: WHILE Blocks

Use the WHILE block to perform looping in a Net.Data macro. Like the IF block, the WHILE block provides the ability to test one or more conditions, and then to perform a block of statements based on the outcome of the condition test. Unlike the IF block, the block of statements can be executed any number of times based on the outcome of the condition test.

You can specify WHILE blocks inside HTML blocks, REPORT blocks, ROW blocks, MACRO_FUNCTION blocks, and IF blocks, and you can nest them. The syntax of a WHILE block is shown in the language constructs chapter of Net.Data Reference.

Net.Data processes the WHILE block exactly the same way it processes the IF block, but re-evaluates the condition after each execution of the block. And, like any conditional looping construct, it is possible for processing to go into an infinite loop if the condition is coded incorrectly.

Example: A macro with a WHILE block


%DEFINE loopCounter = "1"

%HTML(build_table) {
%WHILE (loopCounter <= "100") {
%{ generate table tag and column headings %}
%IF (loopCounter == "1")
<TABLE BORDER>
<TR>
<TH>Item #
<TH>Description
%ENDIF

%{ generate individual rows %}
<TR>
<TD>$(loopCounter)
<TD>@getDescription(loopCounter)

%{ generate end table tag %}
%IF (loopCounter == "100")
%ENDIF

%{ increment loop counter %}
@DTW_ADD(loopCounter, "1", loopCounter)
%}
%}


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]