Forward Iteration
A loop-like construct to iterate on flows
An Iteration on Flows
The Swan language performs operations on sequences of values, called flows; a variable and more generally an expression represents a flow. The forward construct is a loop-like construct allowing to iterate a computation on a finite flow provided by an array.
- inputs: arrays considered as finite flows (for instance both input arrays v and w of size n are considered by the forward body as two flows with n values)
- forward body: each iteration step takes the corresponding input flows from the inputs, performs computations and produces output or local flows.
- outputs: arrays of size n containing all iteration step outputs

An Operational View of the Forward
- inputs: arrays considered as finite flows of n values
- forward body: at each step, the step function takes the nth values of the input arrays and the state Sn to compute the nth values of the output sequence, and the Sn+1 state
- outputs: the array containing the n values or the cumulative results.

Forward Use Cases
The forward construct can be used to perform matrix or arrays manipulations. This construct also allows to implement more complex and different algorithms: all Swan language features can be put in a forward body, and in particular sequential logics (sequences depending on current and past values) like automaton.
In a forward body, the notion of current and previous values refer to the finite flows read or computed by the body. The current value is the value at current iteration step, and a previous value is a value present at a lower iteration step.
For example, an automaton can be evaluated during the n iteration steps, and it is possible to change state at each value in the array. See Using Sequential Operations in a forward where an automaton is used to read a flow of n values contained in an array of size n.
- Single definition of each array element
- Full definition of arrays
- No out-of bounds accesses
- Static array sizes
Forward: A General Iteration Construct
The forward construct is a general construct to implement any iterative algorithm. In particular, an input array is not always required. And as a result of the iterative algorithm, the outputs can be either arrays containing the results of each iteration step (see Forward Construct for Iterative Algorithms ), single flows cumulating the results (accumulators), or only values of the last iteration step.
Multidimensional Iteration
The forward construct allows to iterate on several dimensions, without nesting several forward. An example is presented in Multidimensional Iteration. By default, the forward body is reset at each iteration step, but it can be resumed. The necessity of resuming the body is explained in Forward Resume, as well as the difference between multidimensional forward and nested forward.