Purpose of this chapter.
We will learn about the each statement.
Iteration for the elements of the collection.
The each statement is a statement for iteration over the elements of the collection.
The each statement is the following format.
each ( collection-expression )
The collection-expression must be an expression that returns an instance of a collection. The target collection of an each statement is determined only once before the first loop of the each statement. The target is never changed in the middle of a loop.
The each statement repeats the loop against each element of the target. If all of the loop is end, the execution control will move to the next statement of the keyword.
If the execution control reaches to the continue statement or endeach, the execution control will move to the next statement of the collection-expression. The revaluation of the collection-expression will be not carried out.
If the execution control reaches to the break statement, the execution control will move to the next statement of endeach keyword.
The each statement must be end with the endeach keyword.
A result of the operation on the target during the execution of each statement is undefined. It should be not carried out.
The each keyword indicates that this statement is a each statement.
The collection-expression is an expression for determining the target collection of the each statement. Also, it must be enclosed by parentheses.
The collection-expression must be an expression that returns an instance of a collection class. In other words, it must be a list, dictionary, queue, or stack class.
The target is determined before the first loop of the each statement. And, it is never changed in the middle of a loop.
The each statement is not executed if the target is empty.
If the target collection is a stack, please be careful the order which each statement enumerates the elements. The order that enumerates the elements of the stack is reverse order which the elements were pushed to the stack. That is, the last pushed element becomes the first element to be enumerated.
The end of the each statement.
The end of the each statement is represented by a endeach keyword.
The each statement will generate a scope. Range of the scope is from the each keyword to the endeach keyword.
Variable that has been declared in the each statement can be referred only within the each statement.
Variables that are declared within the scope is cleared every time a new loop begins and will be declared again.
Every time a variable declared, The variable will be assigned the new instance. Also, it does not carry over the previous value to the next loop.
If the execution control reaches to the each keyword, a scope will generate. And, a each statement automatically defines the reserved variable __count and declares the __key and __value.
__count is a variable of int class. It is initialized with the number of loops that starts from zero.
__key is a variable of string class. It is initialized after the evaluation of the collection expression. If the target is a dictionary class, it is initialized with the key of the element. Otherwise, it is an empty string.
The class of the __value is depending on the class of element. The __value is initialized with the element of target after the evaluation of the collection expression.
__count does not cause the overflow like a normal int. If __count has exceeded the maximum value of the int, it will be automatically back to zero.
Please be careful when using the reserved variable in the conditional expression. It must be especially careful when the loop statement is nested. If the loop statements are nested, reserved variable of internal loop statement hides the reserved variable of external loop statement.
The collection express of the each statement is included in the scope that the each statement generates. Therefore, reserved variable in the collection expression is the reserved variable that is defined by the each statement itself.
__key and __value must not use in the collection expression. An exception will be raised. __key and __value are declared at the time of evaluation of the collection expression. However, those are not defined at that point.
Resumption position of the loop.
The collection expression of the each statement is evaluated only once at the beginning of the loop. The result of evaluation is invariant during the loop. Therefore, the resumption position of the loop of the each statement is the next statement of the collection expression.