<< Click to Display Table of Contents >> Navigation: 3. Script Language > Loops, and repeated action > Loops and repeated Commands |
MiniRobotLanguage (MRL)
FOR. Command
For NEX Counting Loop
Intention
If you don't know the terms "Loop" and have no idea what a "FOR ... NEXT" Loop is, please read the section: 1.6.2. Looping around .
This section describes the basics of Loops and the FOR ... NEX - Loop.
The FOR. ... NEX. commands belong together and need to be paired in your script.
For each FOR. there must be a matching NEX. If that is not the case, you will get this error:
There are basically 2 Modes of operation for the FOR. ... NEX. - command.
Mode 1: Floating Point-Mode:
Imagine you want to count from 0.1 to 0.5 in steps of 0.02. Let's try that:
' The 80-bit Floating Point Loop
FOR.$$CNT|0.1|0.5|0.01
DBP.$$CNT
NEX.
ENR.
You see that its easy.
Once the robot finds a "0.x" or a variable, anywhere in your FOR. - line, he will choose internally an 80-Bit IEE Floating Point variable with highest precision.
You get up to 16 significant digits.
It has a theoretical range of: +/- 3.4*10^-4932 to 1.2*10^4932.
This happens automatically, you do not need to think about it.
Mode 2 - 64 bit INTEGER Mode:
In case the robot finds a line like this:
FOR.$$CNT|1|13
or like this:
FOR.$$CNT|1000000000000|1000000000010
He will choose the 64-bit Integer mode. In this Mode you have Integers in the range of: -2^63 to 2^63 -1 that is +/- 64 bit.
In any of the twi Modes, you can specify a negative step.
FOR.$$CNT|13|1|-1
FOR.$$CNT|0.1|0.3|-.001
both are completely valid.
Can you jump out of a loop before it has ended?
Yes, you can. Generally jumping out of loops anytime is completely legal.
However this specific Loop will keep the loop-variable an will not reinitialize it, because the loop did not yet end.
Therefore, in case you want to use that specific loop again, you have to add one line of code. It's not more then a simple variable assignment. Do it, then jumping out is completely legal. Here is an example that shows the needed manual loop-variable initialization.
:again
: $$CNT=13
FOR.$$CNT|13|1|-1
JMP.out
NXT.
JMP.again
This way, you manually reset the Loop-Variable. And that's all to know about jumping around.
How about some speed considerations?
Now lets take a quick look on execution speed. How fast are such loops processed?
Let's try the floating point loop with 1 Million iterations!
We use this script:
: $$TIM=#dtime#
FOR.$$CNT|1|100000.|.1
NEX.
MBX.#dsince#
Of course we need to "compile" the script to get full speed.
It runs in 11.6 seconds!*
Let's see the Integer Mode-Loop.
It needs 10.8 seconds for 1 Million iterations*. This will show you, that Looping comes to you at a very low price in terms of speed!
*These numbers depend heavily on your PC-system. We give them just as an example.
Syntax
FOR.P1|P2[|P3][|P4]
Parameter Explanation
P1 - counter-variable - VAR
P2 - start-number - NUM
P3 - end-number - NUM
P4 - (optional) step - NUM
Speed in Ticks:
This command uses typically around 250 Ticks.
Example
'*****************************************************
' EXAMPLE 1: CTI. Write INI-File
'*****************************************************
'DBM.2
JNF.Move-Console.txt
VAR.§§FIL=?desktop\one.ini
VAR.§§SEC=Section
VAR.§§KEY=Key
VAR.§§VAL=Value
CTI.§§FIL|§§SEC|§§KEY|§§VAL|§§RES
IVV.§§RES=1
PRT. Ini was written with success!
ELS.
PRT. Ini was NOT written with success!
EIF.
' Now we write without check
CTI.§§FIL|§§SEC|§§KEY|§§VAL
' Now we read the file
EXO.§§FIL
MBX. Now we delete this key!
CTI.§§FIL|§§SEC|§§KEY
MBX. Now we delete the whole section!
CTI.§§FIL|§§SEC
END.'*****************************************
' Sample 1: FOR. / NEX
'*****************************************
FOR.§§AAA|1|100
' Loops from 1 to 100
NEX.
FOR.§§AAA|1|100|2
' Loops from 1 to 100 with increment 2
NEX.
FOR.§§AAA|100|1
' Loops from 100 to 1 step -1 can be omitted
NEX.
: $$TAR=100
FOR.$$AAA|$$TAR|1|-0.5
' Loops from 100 to 1 step -0.5
NEX.
FOR.§§AAA|$$STA|$$TAR|§§STP
' Loops from $$STA to $$TAR step §§STP
NEX.
END.
Remarks
FOR. ... NEX. can be nested to unlimited depth.
There is 1:1 paired connection between each FOR ... and the corresponding NEX.
If you do not leave a FOR.-Loop at the NEX.-Command, then the LOOP will stay "active". For example, if you jump out of a FOR.-Loop during the Loop-Run.
If you go into this Loop later, the Robot may be irritated and not reset the Loop-Variable because he thinks "That Loop is still active".
This is normal behvior, because you can also call Subprogrammes or Sub-Skripts from inside a FOR.-Loop.
Therefore in such cases i recommend to add a Initialization to any FOR.-Loop where you want to jump out before the end of the loop. Like this:
' Loop Initilization
VAR.$$LOP=1
FOR.$$LOP|1|99
Limitations:
If you jump out of a FOR...NEX-Loop before it has ended, the Loop-Counter Variable will keep the last value in case you call that same loop again.
In this case, the loop will not start, like normal, but just continue Looping from where it left of, when you jumped out.
Therefore, if you jump out of a FOR. ... NEX:-Loop. And if you want to call that same Loop again, you have to manually initialize the Loop-Variable, immediately before the loop.
Do not jump out of a FOR. - NEX. Loop using JMP. or any Jump, unless you make a manual Loop-initialization. Like in the example below.
'*****************************************
' Sample Jump out of FOR. / NEX
' before End
'*****************************************
: §§PAR=Teststring
' The following Line is necessary if you want to
' jump out of the Loop using JMP. or such commands.
' Otherwise the FOR. would not initialize the
' variable the next time it is used.
'
' This is the needed line:
: §§LOP=1
FOR.§§LOP|1|§§EIM
' This command will jump to Label "eoa"
SAO.nrToN|§§PAR|&H028|eoa|§§LOP
MMV.
NEX.
:eoa
Doing like this, it is completely legal to jump out of as many Loops as you want.
See also:
• DOL. - OOP. - Conditional Loop