在 Net.Data 宏中使用 WHILE 块来执行循环。类似于 IF 块,WHILE 块也 提供测试一个或多个条件的能力,然后基于条件测试的结果执行一个语句块。与 IF 不同的是, 基于条件测试的结果,语句块可被执行多次。
可以在 HTML 块、REPORT 块、ROW 块 MACRO_FUNCTION 块和 IF 块中指定 WHILE 块,并可以 嵌套它们。Net.Data 参考中语言结构章节中显示了 WHILE 块的语法。
Net.Data 处理 WHILE 块的方式与处理 IF 块的方式精确相同,只是在每次执行该块之后重新计算条件。而且与任何条件循环结构相同,如果条件编码不正确的话,就有可能进入死循环。
示例: 具有 WHILE 块的宏
%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)
%}
%}