Developing Apama Applications > Developing Apama Applications in EPL > Defining What Happens When Matching Events Are Found > Defining loops
Defining loops
EPL supports two loop structures, while and for.
The while statement’s BNF definition is:
whileStatement ::= while booleanExpression block
An EPL example is:
integerVariable := 20;
while integerVariable > 10 {
   integerVariable := integerVariable – 1;
   on StockTick("ACME", integerVariable) doAction();
}
The for looping structure allows looping over the contents of a sequence. In BNF its definition is:
forStatement ::= for counter in sequence block
The counter must be an assignable variable of the same type as the type of elements of the sequence. For example:
sequence<integer> s;
integer i;
s.append(0);
s.append(1);
s.append(2);
s.append(3);
for i in s {
   print i.toString();
}
The loop will iterate through all the indices in the sequence, checking whether there are any more indices to cover each time. In the example above, i will be set to s[0], then s[1], and so on up to s[3]. The counter continues incrementing by one each time, and is checked to verify whether it is less than s.size() before a further iteration is carried out. Looping only terminates when the next index would be beyond the last element of the sequence, or equal to size() (since indices are counted from 0).
When the correlator executes a for loop, it operates on a reference to the sequence. Consequently, if the code in the for loop assigns some other sequence to the sequence expression specified in the for statement this has no effect on the iteration. However, if the code in the for loop changes the contents of the sequence specified in the for statement, this can affect the iteration. For example:
sequence <string> tmp := ["X", "Y", "Z"];
sequence <string> seq := ["A", "B", "C", "D", "E"];
string s;
for s in seq {
   seq := tmp;
   print s;
}
The for loop steps through whatever seq referred to when the loop began. Therefore, assigning tmp to seq inside the loop does not affect the behavior of the loop. This code prints A, B, C, D, and E on separate lines.
In the following example, the code in the for loop changes the contents of the sequence specified in the for statement and this affects the behavior of the loop.
sequence<string> seq := ["A", "B", "C", "D", "E"];
string s;
for s in seq {
   seq[2] := "c";
   print s;
}
This code prints A, B, c, D, and E on separate lines.
In the following code, the changes to the contents of the specified sequence would prevent the for loop from terminating.
sequence<string> seq := ["x"];
string s;
for s in seq {
   seq.append(s);
}
EPL provides the following statements for manipulating while and for loops. Usage is intuitive and as per other programming language conventions:
*break exits the innermost loop. You can use a break statement only inside a loop.
*continue moves to the next iteration of the innermost loop. You can use a continue statement only inside a loop.
*return terminates both the loop and the action that contains it.
Copyright © Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or its Subsidiaries and or/its Affiliates and/or their licensors.
Use, reproduction, transfer, publication or disclosure is prohibited except as specifically provided for in your License Agreement with Software AG.