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!)

 
Update Information

 Version

 Title

Date  

 Details

49.32

 Internal Message Items - Increased nesting depth/loops/functions from 24 to 28.

14 Feb 13 

Show

* Increased nesting depth/loops/functions from 24 to 28.
* Current number of used msg items can be read in S32 Variable SYSINTMSG.

49.08

 Not Function Error - Fixed error which was due to a pointer error when changing/refreshing a page without page loop

11 Jul 12 

Show

Fixed "Not Function" Error which was due to a pointer error when changing/refreshing a page without page loop. This problem could cause other unexplained errors. This bug was present in the code from before V00.47.24.

49.03

 LOOP() - ''Array Error - Subscript Out Of Range'' message

01 Jun 12 

Show

"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.* 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!)

* 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.

47.24

 Loops - Loop duration of ''FOREVER'' now accepted for all loops (not just page loop).

31 Oct 11 

Show

Loop duration of "FOREVER" now accepted for all loops (not just page loop)
* Touch screen events are processed every loop.

47.24

 Exit() - end loops and functions - Exit command added to exit loops or functions

31 Oct 11 

Show

Exit command added to exit loops or functions
> exit(); // exit current loop/function
> exit(name); // exit nested loops/functions up to and including loop/function with name
* Examples:
> loop(lp1,FOREVER){ calc(x,y,z,"+"); if(x=5?[exit(lp1);]); } // exit loop when x=5
> loop(lp2,FOREVER){ calc(x,y,z,"+"); if(x=5?[exit(lp2);]); } // exit loop when x=5 (as above)
> func(fn1) { if(x=5?[exit(fn1);]); ...... } // exits function when x=5 without running rest of code
> func(fn2) { loop(lp3,100){ load(rs2,"*"); if(quit=1?[exit(fn2);;]); // sends 100 *'s through RS2 unless quit is set to 1, then loop and the function are exited (A screen refresh occurs before the exit)

* Note, if the name provided in the exit(name); command does not exist in the current function/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.

45.00

 Pointers - Fixed a potential pointer issue in the main page loop.

10 Jun 11 

Show

Fixed a potential pointer issue in the main page loop.

45.00

 Loops - Added capability for nested loops and loops in functions.

10 Jun 11 

Show

Added capability for nested loops and loops in functions
Loop Example 1
var(ii,0,U8);
var(jj,0,U8);
var(kk,0,U8);
func(fn1)
{
load(ii,0);
loop(lp0,10)
{
load(jj,0)
loop(lp1,10)
{
load(kk,0);
loop(lp2,10)
{
load(rs2,ii,",",jj,",",kk,"\\0d\\0a");
calc(kk,kk,1,"+");
}
calc(jj,jj,1,"+");
}
calc(ii,ii,1,"+");
}
load(rs2,"\\0d\\0a");
}

Outputs: 0,0,0\\0d\\0a0,0,1\\0d\\0a0,0,2\\0d\\0a...9,9,9\\0d\\0a\\0d\\0a

Loop Example 2

key(k0,[loop(klp,10){load(rs2,"*");}load(rs2,"\\0d\\0a");],480,136,TOUCH);

Outputs on key press: **********\\0d\\0a

21.00

 FUNC LOOP - Allow single line FUNC(){} and LOOP(){}

30 Jul 10 

Show

Allow single line FUNC(){} and LOOP(){}
e.g. FUNC(Help) { SHOW (HelpPage); }