Procedural execution in eXPL is constrained by it's unification engine to progress in a linear fashion. A loop has just one point of entry and departure so can be inserted in a program anywhere without causing a discontinuity. Nesting of loops inside loops is also not a problem.
There are 2 types of loops:
- Conditional Block - A short circuit expression controls entry into a block from which the program exits when the end of the block is
reached. The block is a one-transit loop. There are 2 short circuit operators,
?and:. The?operator has already been encountered and has the sense "short circuit on false". The:operator has the opposite sense - "short circuit on true". To short circuit the conditional block means to not enter it. - Unconditional Loop - There is no expression controlling entry into the loop and the only way out of the loop is by a short circuit operation. Beware!. The loop can become an infinite loop if a bug in the code prevents any of the short circuit expressions inside the loop from evaluating an exit condition.
A loop is only allowed in a calculator and is defined in the code as a term sequence inside a set of braces {}.
The following example has a single unconditional loop with a ? expression exit.
Unconditional Loop Example
The Factorial application of tutorial8 generates the factorial of 4 (2*3*4 = 24) and 5 (2*3*4*5 = 120).
integer n,
. integer i = 1,
integer factorial = 1,
{
? i++ < n
);
query factorial4(factorial)(n = 4);
query factorial5(factorial)(n = 5);
Conditional Block Example
Application CalculateSquareMiles2 in tutorial8 contains a conditional sequence to adjust nation surface area from km2 to square miles if the country is USA.
country { "United States" , "Australia" },
...
? country == "United States"
{
surface_area *= 0.3861,
units = "mi2"
}
The result shows surface areas for Australia and USA in Km2 and mi2 respectively:
country=United States, surface_area=3,795,946.011, units=mi2
