for each (object) in (list)

This construct specifies the actions that must be performed to 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.

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':
        - set decision to 5;

The following rule shows two nested for each statements. The called <variable> construct is used so that each customer can be explicitly referred to from within the nested for each loop.

if
   the category of customer is Gold
then
   for each customer called c, in 'senior customers':
      - for each account in 'new accounts':
         - set decision to the mailing address of c;