<< Click to Display Table of Contents >> Navigation: 1. Starting Guides > 1. Starting Guide |
Starting Guide
Looping around
Understanding Loops: The Power of Repetition
A conveyor belt is a good example of a repetitive activity
Looping around - what is "a Loop"?
Computers excel at performing repetitive tasks, tasks that might be monotonous if done manually. Let's delve into the concept of 'loops', a fundamental principle in programming that enables this powerful repetition. We'll begin with a straightforward example to ease your understanding.
Assume you want to count from 1 to 50. In a script you could do it like this:
' This the most complicated way to do it.
' Init counter variable
: §§CNT=1
' This is just a Label (jump mark)
:Loop
' Print the actual number
PRT.§§CNT
'Increment the number (do a +1)
VIC.§§CNT
' Leave the Loop, if we are above 50
IVV.§§CNT>50
GTO.exit
EIF.
' Jump back to the start of our Loop
GTO.Loop
' Another Label
:exit
This is really a long script for a loop!
Lets try to a shorter version. We can change the "IVV." to a JIV.-command.
' Init counter variable
: §§CNT=1
' This is just a Label (jump mark)
:Loop
' Print the actual number
PRT.§§CNT
'Increment the number (do a +1)
VIC.§§CNT
' Leave the Loop, if we are above 50
JIV.§§CNT>50|exit
' Jump back to the start of our Loop
GTO.Loop
' Another Label
:exit
This will work but is not really a lot shorter. If we choose a JME. instead we will save one more line, because JME. make an Auto-Increment if we use the ">" operator. I'll also add some statements that do a time-measurement.:
' Remember actual time
VAR.§§TIM=#time#
' Init counter variable
: §§CNT=1
' This is just a Label (jump mark)
:Loop
' Print the actual number
PRT.§§CNT
' Leave the Loop, if we are above 50 and Auto-Increment
JME.§§CNT>50|exit
' Jump back to the start of our Loop
GTO.Loop
' Another Label
:exit
' Get time since last call to #dtime#
VAR.§§TIM=#since#
' Tell me how long time the execution needed.
MBX.§§TIM
If you run the script in the editor, you can see that we needed for example about 4 seconds. If you would run it as a compiled executable, it will easily be 100 or more times faster.
The FOR .. NEX.-Loop
At this point, we could get a bit simpler when using JOR. I'll leave that for yourself to try. Instead we will now take the proper way of a "counting loop". It is the "FOR. ... NEX."-Loop.
Imagine you want to count from 1 to 10. How would you do that?
You would start counting with 1 ... 2 ... until you reach 10.
That is what the FOR. ... NEX. command is doing. If we count like In our example, from 1 to 10, it would look like this:
' This will count from 1 to 10
FOR.$$CNT|1|10
' ... we can do something with the number in $$CNT
NEX.
It will start counting from a given value (in our example it is 1), until another given value (in the example 10) is reached. All the way, we have the actual value in the variable $$CNT.
FOR. .. NEX. has several Modes of Operation to match all your needs.
First, you can specify a STEP-value. It will look like this:
FOR.$$CNT|1|10|3
DBP.$$CNT
NEX.
ENR.
You will see an Output in the Editor-Debug-Area like this:
The [9] tells you that the DBP.$$CNT command was in line 9 of our script. More important are the other numbers. They start with 1 and then the Counter always adds 3.
That's exactly the step-value we have given!
Here is another example, like the one we made before.
' Start our stop-watch
VAR.§§TIM=#dtime#
' Init counter variable
FOR.§§CNT|1|50
' Print the actual number
PRT.§§CNT
NEX.
' Get the time how long we needed.
VAR.§§TIM=#dsince#
MBX.§§TIM
ENR.
How many seconds does it need on your computer? Using the proper counting loop, we saved a lot of lines and made all the same in for example 2 seconds!
Do we need loops to wait for something?
Lets think of a situation, where we are waiting for files to appear or disappear.
Normally we do not need to use a loops to wait for one event to happen. The reason is that such loops are already built in. For example, to wait for a file, we can simply use the WFF. - "Wait For File" Instruction. We would write:
WFF.c:\Logfile.txt
to wait for the specified file to appear. In the opposite case, that we need to wait for a file to disappear, we would add a "<>" like that:
WFF.<>c:\Logfile.txt
This will wait for the file to "Not be there". You can also specify a timeout and test if the timeout time was reached, using the ITO. "If Time Out" - Instruction.
The same applies for many other tasks, where we can use the "Wait-For" Instruction Group.
But what do we do, if we want to wait for "one of several files" to appear?
I am using files in this example, but the same applies to any other system-object. Like windows, processes, or whatever.
To do this we must construct our own loop. This time we do not need to count anything, We need just to "Loop in a circle" until things happen.Like an endless loop. Until something happens. When this thing we are waiting for happens, then we leave the loop and do something.