1.6.3 Flexible Loops

<< Click to Display Table of Contents >>

Navigation:  1. Starting Guides >

1.6.3 Flexible Loops

1.6.3. Flexible Loops

Previous Top Next


Starting Guide

 

Flexible Loops

Understanding Loops: The Power of Repetition

 

clip0529

A conveyor belt is a good example of a repetitive activity

 

 

The conditional DOL. - OOP. Loop

 

For such situations you have the DOL. OOP. construct. Lets make a endless loop.

 

DOL.1

 PRT. I am in here forever!

OOP.

ENR.

 

Try this in the Editor, it will run in an endless loop, until you stop it. This was just for you to see how it works. Now we'll use it for a real world example.

 

In reality you will have conditions when you jump out of this loop. Lets take a situation where we wait for two files. If one of them appears, we will leave the waiting loop.

 

VAR.§§FIA=?pfad\myfile1.txt

VAR.§§FIB=?pfad\myfile2.txt

' Here is our endless loop

DOL.1

 PRT. I am here!

' We test for File1

 IEF.§§FIA

   GTO.exit

 EIF.

' We test for File2

 IEF.§§FIB

   GTO.exit

 EIF.

 ' We'll give the CPU a tenth of a second sleep time

 ' always do this in endless loops to save CPU time.

 ' for other running programs

 PAU.0.1

OOP.

:exit

MBX. A file appeared!

ENR.

 

You can run this and when the file appears, the script will exit its endless loop.

While this script is completely correct, we will change it a bit. So we do not need an label "exit", and no GTO. "GOTO". We use the power of DOL. ... OOP.

These two statements allow you to construct whatever Loop you want.

 

 

VAR.§§FIA=?pfad\myfile1.txt

VAR.§§FIB=?pfad\myfile2.txt

' This variable will be set to 1 if the file is found

: §§XIT=0

' Loop will run WHILE §§XIT=0

DOL.§§XIT=0

 PRT. I am here!

' We test for File1

 IEF.§§FIA

   : §§XIT=1

 EIF.

' We test for File2

 IEF.§§FIB

   : §§XIT=1

 EIF.

 ' We'll give the CPU a tenth of a second sleep time

 ' always do this in endless loops to save CPU time.

 ' for other running programs

 PAU.0.1

OOP.

:exit

MBX. A file appeared!

ENR.

 

 

We would construct the same loop better as a "REPEAT ... UNTIL" loop. Because that is what we are doing. We REPEAT ... UNTIL a file is found. Therefore we change it again.

 

 

VAR.§§FIA=?pfad\myfile1.txt

VAR.§§FIB=?pfad\myfile2.txt

' This variable will be set to 1 if the file is found

: §§XIT=0

' Here is our loop

DOL.

 PRT. I am here!

' We test for File1

 IEF.§§FIA

   : §§XIT=1

 EIF.

' We test for File2

 IEF.§§FIB

   : §§XIT=1

 EIF.

 ' We'll give the CPU a tenth of a second sleep time

 ' always do this in endless loops to save CPU time.

 ' for other running programs

 PAU.0.1

' This loop will repeat UNTIL §§XIT=1

OOP.(§§XIT=1)

:exit

MBX. A file appeared!

ENR.

 


 

CPU-time considerations

 

Now let us see an endless loop:

 

:Loop

... do something

GTO.Loop

 

This is just an example for an endless loop. You have many more options to build loop.

See here: 1.6.1. Program Flow Control

 

Now if you run this as a compiled executable it will run very fast!

Here is an example:

 

: $$CNT=70000

: $$TIM=#dtime#

:Loop

JIZ.$$CNT<out

GTO.Loop

:out

: $$TIM=#dsince#

DMP.

MBX.We needed $$TIM

ENR.

 

This Loop will be processed about 70000 times in about a second, see picture below.

This needs a good part of possible CPU-time. If you have a multicore CPU, it may take a complete core just to run in a circle.

 

In most cases you wait for something to appear or something to disappear. You don't need 70000 checks per second, 100 would be more then enough.

Using the PAU. - Instruction, you can slow the process down and give CPU-time to other running processes.

 

 

graphic

 

 

We can change the script like this:

 

: $$CNT=500

: $$TIM=#dtime#

:Loop

JIZ.$$CNT<out

PAU.1|ms

GTO.Loop

:out

: $$TIM=#dsince#

DMP.

MBX.We needed $$TIM

ENR.

 

and get this result:

 

graphic

 

This time we just make 500 rounds (still a lot!) and in each round a rest of 1 ms.

1 ms = 1/1000 th part of a second.

 

Lets get it down to about 50 rounds per second. This way we can finetune the CPU Usage of our script.

 

: $$CNT=50

: $$TIM=#dtime#

:Loop

JIZ.$$CNT<out

PAU.19|ms

GTO.Loop

:out

: $$TIM=#dsince#

DMP.

MBX.We needed $$TIM

ENR.

 

this will get the expected result, 50 rounds per second with a waiting time of about 19 ms per round. Of course these time differ with the used system, CPU etc. You may get other values on your system.

 

graphic

 

However, this example should get you started how to "fine tune" Loops, for example in case you run server-based scripts.