[NAME] ALL.dao.control.for [TITLE] For Looping Control [DESCRIPTION] Dao supports three different styles of for loops: * For-in loop; * Range for loop; * C style for loop; 0.1 Definition 1 ForIn ::= 'for' '(' ['var'|'invar'] Identifier 'in' Expression {';' Identifier 'in' Expression} ')' 2 ControlBlock 3 4 RangeFor ::= 'for' '(' ['var'|'invar'] Identifier '=' Expression ':' [Expression ':'] Expression ')' 5 ControlBlock 6 7 CFor ::= 'for' '(' [ LocalVarDeclaration ] ';' [ Expression ] ';' [ ExpressionList ] ')' 8 ControlBlock 9 10 ForStmt ::= ForIn | RangeFor | CFor 0.2 For-In For-in loop can normally used to iterate over the items of a container object. It is probably most useful with list, 1 var numbers = { 1, 2, 3, 4, 5 } 2 for( num in numbers ) io.writeln( num ) For-in loop can also be used with map or hash map, 1 var mapping = { 'AA'=>11, 'BB'=>22, 'CC'=>33 } 2 for( pair in mapping ) io.writeln( pair ) When iterating over a map or hash map, the iteration variable (pair) will store each pair of key and value as a tuple. In a single for-in loop, multiple in expressions can be used to iterate over multiple obj ects simutaneously, 1 var numbers = { 1, 2, 3, 4, 5 } 2 var mapping = { 'AA'=>11, 'BB'=>22, 'CC'=>33 } 3 for(var num in numbers; var pair in mapping ) io.writeln( num, pair ) 0.3 Range For A range for loop uses an arithmetic progression condition to control the loop. The arithm etic progression condition usually consists of an initial value, an optional step value a nd a stopping value. 1 for(var index = 1 : 2 : 10 ) { # step value = 2; 2 io.writeln( index ) 3 } 4 for(var index = 1 : 10 ) { # step value = 1; 5 io.writeln( index ) 6 } The loops will start with index=1, and with each loop cycle, index is increased by two o r one, when index become greater than 10, the loop will stop. 0.4 C Style For C-style for loop is the most flexible for loop construct. It generally looks like the fol lowing: 1 for( init; condition; step ){ 2 block; 3 } The essential execution logic or steps of for statement is the following, 1. Execute the initial expression init; 2. Check the condition expression condition; 3. If true continue to the next step, otherwise exit the loop; 4. Execute the code block block; 5. Execute the step expression step and go to 2; The detailed steps may depends on the implementation but the basic execution logic is the same. Example, 1 for(var i=0; i<3; ++i){ 2 io.writeln( i ); 3 } 4 for(var i, j=0; i<10; ++i, j+=2){ 5 io.writeln( i, j ); 6 }