循环允许逻辑部件多次执行同一段 EGL 代码。通常使用循环来处理数组,这是因为循环允许您对数组中的每个元素执行同一个操作。EGL 包括三种类型的循环,在使用其它编程语言时您可能已经熟悉它们了:while、for 和 forEach。
在此练习中,将创建这样的一个 PageHandler:它使用 EGL getSize() 函数来找出数组中的元素个数。然后,对数组中的每个元素执行一次 for 循环。这样,PageHandler 就可计算出数组中符合特定条件的记录数。
有关 EGL 中的循环的更多信息,请参阅帮助主题 while、for 和 forEach。
在下面的步骤中,创建这样的一个 Web 页面:使用循环对数据库中特定州的实例进行计数。
LoopTest
package pagehandlers;
import data.*;
PageHandler LoopTest
{view="LoopTest.jsp", onPageLoadFunction=onPageLoad}
//Variables
listOfAllCustomers customer[];
inputState char(2)
{displayName = "Enter a state:"};
outputMessage char(100);
outputSum int;
Function onPageLoad()
customerLib.getAllCustomers(listOfAllCustomers);
end
Function testSimpleLoop()
//Initialize variables
counter int = 1;
outputSum = 0;
outputMessage = "";
numberOfRecords int = listOfAllCustomers.getSize();
//Find the number of times the input state is in the customers array
for (counter from 1 to numberOfRecords by 1)
if (listOfAllCustomers[counter].STATE == inputState)
outputSum = outputSum + 1;
end
end
//Create an output message describing the number of customers found
if (outputSum == 0)
outputMessage = "No customers are from the state " + inputState;
else
outputMessage = "There are " + outputSum + " customers from " + inputState;
end
end
end
以下是一些有关刚才插入的代码的技术说明:
“插入列表控件”窗口看起来应如下所示:

该页面看起来应如下所示:
