Each statement

It goes round the elements of the collection.


Table of contents:


Format

each

Collection expression

Loop procedure

Patrol order

Scope

Reserved variable

Statement

endeach

Example

Format

The format is:


each ( Collection-expression )

...

endeach


Each statement goes round the elements of the target collection. Once all the elements have been cycled, execution moves to the next of the endeach.

If the execution reached "continue" or "endeach", execution moves to the next of the collection expression.

If the execution reached "break", execution moves to the next of the endeach.


If the target collection is empty, the each statement will not be executed.

The result of manipulating the target collection in each statement is undefined. There is no telling what will happen.

each

A keyword that indicates that it is an each statement.

Collection expression

The collection expression determines the target collection for each statement. It must return an instance of the collection class.

Collection classes are:


list class

dictionary class

queue class

stack class

Classes which used {} for these.

If the collection expression returns a proxy class, the entity of the proxy is automatically used. The result is the same as calling the Entity getter of the proxy class.


list{int} il = new list{int}("{1, 2, 3, 4, 5}");

proxy pro = new proxy(il);

each(pro) // pro.Entity is used.

...

ee


The entity of the proxy must return an instance of the collection class.

Loop procedure

♦1: At the beginning of the statement, the target collection is determined from the collection expression. This is done only once at the beginning.


♦2: An element that has not been patrolled takes out. If all elements have been patrolled, the execution moves to the next of the endeach.


♦3: Statements within the each statement are executed.


♦4: When the execution reaches the "continue" or "endkeepon", the execution goes back to the ♦2.

Patrol order

Patrol order are:


list class: The order in which elements were added.

dictionary class: The order in which elements were added.

queue class: The order in which gets by the dequeue() method.

stack class: The order in which gets by the Pop() method.

Scope

The each statement generates a scope.


Variables declared in the scope are re-declared per loop and new instances are assigned.

It does not carry over the value of the previous loop to the next loop.

Reserved variable

When execution reaches the each, a scope is generated. Then, reserved variables __count and __index are defined and __key and __value are declared.

__count and __index are variables of the int class. They are initialized with the number of loops starting from zero.

__key is a variable of the string class and is initialized after the evaluation of the collection expression. If the target collection is a dictionary class, it will be initialized with the key of the element. Otherwise, it is an empty string.

The class of __value depends on the element. It is initialized with the element after the evaluation of the collection expression.

__count and __index do not cause an overflow. If exceed the maximum value of the int, automatically return to zero.


The scope has already been generated when evaluating the collection expression. Therefore, if you use a reserved variable in the evaluation, the reserved variable is the one defined by the current each statement.

Please don't use __key and __value for the collection expression. An exception will occur.

Statement

Statements within a each statement are optional. You can place statements as many as you want.

endeach

This is a keyword that indicates the end of the each statement.

You can use the abbreviation "ee" instead of "endeach". Note that "ee" is also a keyword.

Example

1:

class example

2:

open method void someMethod()

3:

message m; // dialog box.

4:

list{int} il = new list{int}("{1, 2, 3, 4, 5}");

5:

each (il)

6:

m.Show(__value); // 1, 2, 3, 4, 5

7:

endeach

8:

em

9:

ec

Copyright © Rice All rights reserved.