Please enable JavaScript to view this site.

DAQFactory User's Guide

Navigation: 5 Sequences & Scripting

5.14 Sequence looping with the while statement

Scroll Prev Top Next More

The simplest way to make sequence steps execute repeatedly is to use a while loop.  For example:

 

while (Input[0] <= 10)

  Output = 10 - Input[0]

  delay(1)

endwhile

The steps between the while and the endwhile will execute continuously until Input becomes greater than 10.  In this particular case, Output is set to the difference between Input and 10 once a second until Input is greater than 10.  The expression in the while can be more complicated then this example, and can also be very simple:

 

while (1)

  read (Input)

  delay(1)

endwhile

This sequence will read the Input channel once a second until the sequence is manually stopped.  Because 1 is the same as true, this loop is an infinite loop, but because we put a delay statement inside the loop, this will not be a problem.  If we forgot the delay statement, the loop would execute very rapidly and probably hang DAQFactory.  With some rare exceptions, you should always put either a delay or wait statement inside of any loop.

Inside of the while loop, you can add a break statement to force the sequence to leave the loop:

 

while (1)

  if (Input[0] > 10)

    break

  endif

  Output = 10 - Input[0]

  delay(1)

endwhile

This sequence will actually do the exact same thing as our first while example.  The while statement itself always evaluates to true, but inside the while we have an if statement that looks at Input and breaks out of the while loop if Input goes greater than 10.

Another useful statement inside of a while loop is continue.  Normally all the steps between the while and the endwhile execute each loop.  The continue statement causes the sequence to jump back to the while statement, reevaluate the condition and either start the loop over, or break out of it:

 

while (Input[0] <= 10)

  if (Input[0] > 9)

    delay(1)

    continue

  endif

  Output = 10 - Input[0]

  delay(1)

endwhile

This sequence works just like the one above it, except that the Output is not adjusted once Input goes above 9.  The if statement looks for Input to go above 9, and when it does so, calls the continue statement which jumps the sequence back to the while statement.  Notice the delay statement inside the if as well.  If we did not put this in we'd have a fast infinite loop occur once Input raised above 9.