Home About Rice Downloads Documents Others Cooker CookerGX
Documents  >  Tutorial  >  fromtostatement
fromto statement

Purpose of this chapter.

We will learn about the fromto statement.

Loop by the index.

The fromto statement is a statement to control the loop depending on the index.

The fromto statement is the following format.


fromto ( start-value, end-value )

statement...

endfromto


The start-value and end-value must be an expression that returns an instance of a int class. Both value are evaluated only once before the first loop of fromto, the values are never changed in the middle of a loop.

The fromto statement repeats the loop if the index which is determined by the start and end values satisfies the conditions. If the index do not satisfies the condition, the execution control will move to the next statement of the endfromto keyword.

If the execution control reaches to the continue statement or endfromto, the execution control will move to the next statement of the end-value after evaluating the current index. The revaluation of the start-value and end-value will be not carried out.

If the execution control reaches to the break statement, the execution control will move to the next statement of endfromto keyword.

The fromto statement must be end with the endfromto keyword.

fromto keyword.

The fromto keyword indicates that this statement is a fromto statement.

Start and end values.

The start-value and end-value are expressions for determining the index of a fromto statement. Also, those must be enclosed by parentheses and separated by comma.

The start-value and end-value must be an expression that returns an instance of a int class. Both value are evaluated only once before the first loop of fromto, the values are never changed in the middle of a loop.

If the start value is less than or equal to the end value. The index that was initialized to the start value is added 1 for each loop. If the index is greater than the end value, the execution control will move to the next statement of endfromto.

If the start value is greater than the end value. The index that was initialized to the start value is subtracted 1 for each loop. If the index is less than the end value, the execution control will move to the next statement of endfromto.

If the start value is equal to the end value, the loop of fromto statement is executed only once. In other words, the loop of fromto statement is executed once at least.

The end of the fromto statement.

The end of the fromto statement is represented by a endfromto keyword.

Scope

The fromto statement will generate a scope. Range of the scope is from the fromto keyword to the endfromto keyword.

Variable that has been declared in the fromto statement can be referred only within the fromto 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.

Reserved variable.

If the execution control reaches to the fromto keyword, a scope will generate. And, a fromto statement automatically defines the reserved variable __count and declares the __index.

__count is a variable of int. It is initialized with the number of loops that starts from zero.

__index is a variable of int. It is initialized with the current index value after the evaluation of the start and end values.

__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 start and end values of the fromto statement are included in the scope that the fromto statement generates. Therefore, reserved variable in the start and end values is the reserved variable that is defined by the fromto statement itself.

__index must not use in the start and end values. An exception will be raised. __index is declared at the time of evaluation of the start and end values. However, it is not defined at that point.

Resumption position of the loop.

The start and end values of the fromto statement are 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 fromto statement is the next statement of the end value.

Next
Previous
Copyright © Rice All rights reserved.