The foreach Loop

The foreach control structure is similar to the foreach loop in Perl. The loop iterates through a list, and executes the associated code each time. The current item in the list is stored in a temporary variable as supplied by the user:

foreach temporary_variable in list_variable: code.

An example of the foreach loop is shown below.

# so, for example, if we have a variable called agent and a list
# of objects stored in agentList:

foreach agent in agentList: {
        print (agent get-location).
}