Command Loop
 
Description Repeats the specified actions a number of times in a PAGE then continue. Max 28 nested loops or functions - v49.32. The value for Var1 can be a number from 1-65000 or the text FOREVER. You can exit a LOOP using the command EXIT(Name); Loops can be nested and used in PAGES or FUNCTIONS.
 
Syntax/Parameters LOOP(Name,Var1){...........}
 
Options

EXIT(Name) - end loops - v47.24
> EXIT(name); // exit nested loops up to and including loop with name
Examples:
> LOOP(lp1,FOREVER){ CALC(x,y,z,"+"); IF(x=5?[EXIT(lp1);]); } // exit loop when x=5
Note, if the name provided in the EXIT(name); command does not exist in the current loop nesting, then all loops
and functions are exited up to the top level. It is not possible to exit the page loop in this way.


Precautions when using LOOP() including "Array Error - Subscript Out Of Range" message
At the start of each pass through a loop, a check is performed to see if a touch screen key is being pressed and, if it is, then the associated touch key function is called. Caution must be observed with the touch key function to not modify variables that are being used within the loop otherwise undesired results can occur which can be difficult to spot or result in an error message.

Good coding practice
  > Make sure variables used in a loop are not modified from a touch key function (unless this is a desired action)
  > If a variable does need to be changed then set a 'flag' in the key function and test the flag in the page loop and make the change there instead.

 
Example LOOP(MyLoop,12){SHOW(Page1);WAIT(100);SHOW(page2);WAIT(100);}  //repeat 12 times
LOOP(MyLoop,FOREVER) {SHOW(Page1);WAIT(100);SHOW(page2);WAIT(100);}

Loop Example 1
   FUNC(fn1)
     {
     VAR(ii,0,U8);
     VAR(jj,0,U8);
     VAR(kk,0,U8);
     LOOP(lp0,10)
       {
       LOOP(lp1,10)
         {
         LOOP(lp2,10)
           {
           LOAD(RS2,ii,",",jj,",",kk,"\\0d");
           CALC(kk,kk,1,"+");
           }
         CALC(jj,jj,1,"+");
         }
      CALC(ii,ii,1,"+");
      }
    LOAD(RS2,"\\0d");
    }
     RS2 Outputs: 0,0,0\\0d0,0,1\\0d\\0,0,2\\0d...9,9,9\\0d<\\0d


Loop Example 2
    KEY(k0,[LOOP(klp,10){LOAD(RS2,"*");}LOAD(rs2,"\\0d\\0a");],480,136,TOUCH);
    RS2 outputs on key press: **********\\0d\\0a


    Restriction: If the LOOP() command is within a function called from a KEY() command then further key presses will be ignored.
    Each touch key press function must be processed to completion before another can be processed.
    Please refer to the project example 'keyboard' for the technique to process keys.

Example 1 - Variables:
  VAR( varX, 0, U8 );
  // We have a simple function...
  FUNC( fnTest1 )
  {
    LOAD( varX, 0 );
    LOOP( lpTest1, 10 )
    {
      // [Touch Keys are effectively tested here]
      LOAD( RS2, varX );
      CALC( varX, varX, 1, "+" );
    }
  }
  // In a page we have...
  KEY( kyTest1, [ LOAD( varX, 0 ); ], 100, 100, TOUCH );
  // Normally we would get 0123456789 sent out of the RS2 port each time fnTest1 is run
  // If however the key kyTest1 is pressed when the loop is being run then the output may be changed to 0123012345!

Example 2 - Arrays:
  VAR( varArr, 0, U8, 5 );
  VAR( varY, 0, U8 );
  // We have another simple function...
  FUNC( fnTest2 )
  {
    LOAD( varY, 0 );
    LOOP( lpTest2, 5 )
    {     
     // [Touch Keys are effectively tested here]
      LOAD( RS2, varArr.varY );
      CALC( varY, varY, 1, "+" );
    }
  }
  // In a page we have...
  KEY( kyTest2, [ LOAD( varY, 0 ); ], 100, 100, TOUCH );
  KEY( kyTest3, [ LOAD( varY, 3 ); ], 100, 100, TOUCH );

  // Normally we would get the contents of varArr.0 varArr.1 varArr.2 varArr.3 varArr.4 sent out of the RS2 port each time fnTest2 is run
  // If however the key kyTest2 is pressed when the loop is being run then the output may be changed to varArr.0 varArr.1 varArr.0 varArr.1 varArr.2!
  // Or, an error when kyTest3 is pressed giving varArr.0 varArr.1 varArr.3 varArr.4 ** Array Error - Subscript Out Of Range ** (ie varArr.5 doesn't exist!)