for each (object) in (list)
This construct specifies the actions that must be done for every element of a collection.
Purpose
You use this construct to apply one or more actions to all objects in a collection.
Syntax
for each <object> [called <variable>,] in <list>:
- <action>*;
Description
The construct should be immediately followed by a colon (:) and a list of one or more actions, each on a separate line, each preceded by a dash. End each action statement with a semi-colon (;).
The implicit variable ‘this <object>’
can be used in each action statement within the for each loop to refer to the
current object. If you nest a for
each statement inside another, you can use the called construct to define an
explicit name for the object in the 'outer' for each loop in order to reference
it clearly from within the nested for
each loop.
Example
The following rule shows how to apply a discount to all the expensive items in a customer’s shopping cart, if the customer is a Gold customer.
definitions
set ‘smith’ to a customer ;
set ‘expensive items’ to all items
in the items of the shopping cart of smith
where the price of ‘each item’ is greater than 1000;
if
the category of smith is Gold
then
for each item in ‘expensive items’:
- apply 5% discount to this item;