Home News DOC Class DL Others Cooker
Documents  >  Tutorial  >  Each
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 of the each statement 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 a each statement.

Collection expression

The collection expression is an expression that determines the target collection for each statement. It must be an expression that returns an instance of the collection class.

Collection classes are:


list class

dictionary class

queue class

stack class

A class that used "{}" for the above classes.

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: It determines the target collection from the collection expression. This is done only once at the beginning.


♦2: Take out an element that is not patrolled. If all the elements are patrolled, the execution moves to the next of the endeach.


♦3: The 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

It goes round the elements in the following order.


list class: The order in which the elements were added.

dictionary class: The order in which the 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.Pop

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. And, the reserved variable __count is defined and __key and __value are declared.

__count is a variable of the int class. It is 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 elements. It is initialized with the elements after the evaluation of the collection expression.

__count does not cause an overflow. If __count exceeds the maximum value of the int, it returns to zero.


The scope has already been generated when evaluating the collection expression. Therefore, if you used 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

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

endeach

It 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

Next
Previous
Copyright © Rice All rights reserved.