For Loop

Navigation:  Widget Designer > Scripting > Script Language > Programming Statements >

For Loop

prev main next

Navigation:  Widget Designer > Scripting > Script Language > Programming Statements >

For Loop

prev main next

Please wait. Due to its content this page might take a while to load...

If you want to repeat a certain script segment several times or based on a time that can change through a variable, then For Loop scripts are ideal to solve such a task efficiently.
Here is a quick example of how this looks like in a WD script:

Basic syntax

For i = Value1 to Value2 optional Step Value {
 Script
}

General Scripting Hints

It does not matter whether the curly brackets are in the same or next line, but it is recommended to add new lines and also tabs for a clear and easy to read arrangement.
Code blocks can be in/out-dented using [Tab] or [Shift + Tab].

If you like to comment a line out without getting errors in the Debug Logger, use this syntax:
// one line that should not be processed
/* several lines

that should not

be processed */

 

Next to the Debug Logger, the "Running Scripts" dialog (from the Scripting menu) might be helpful. It shows which scripts are currently running and offers the option to cancel them via the right-click menu. That way, you can cancel a for loop e.g. if it takes too long. Further down, the "break" statement is explained.

Script Example

For i = 1 to 10 {
 Label1.Text = Now.ToString
 WDWait(1)
}

This script will execute 10 times the code between the curly brackets, i.e. display the current time in Label1 and then wait one second before it does that again.

A special local variable is defined in the beginning and increases automatically in every loop. The default name for the iteration variable is "i" and as you see below, you can use it in your script, e.g. as the text for a label or as the ID for a Fader.

You can use any valid name for the iteration variable, it doesn't necessarily have to be "i", but please do not use already defined variables as iteration variables, too.

If you want to run nested For Loops, you would have to use a different iteration variable for each loop.

 

As te iteration variable "i" it is an integer, you can also do math with it. In the following example we use the command WDFaderValue(ID,Value)to set the Faders 1-10 to a value which is ten times its own ID (so Fader with ID 1 is set to 10 etc.)

 

For i = 1 to 10 {
 Label1.Text = "Let's move Fader" + i
 WDFaderValue(i,i*10)
 WDWait(1)
}

You can also use variables instead of a defined value, which is especially interesting for "end" values. The next example shows a for loop that runs as many times as a list variable has elements.

var list = ["Apple","Bee","Coke"] //defines a local list variable with 3 elements => for loop will run 3 times
for i=1 to list.Count {
 WDLabelText(i,list[i-1])

}

The result of this script is, that the first Label displays "Apple", the second Label "Bee" and the third one "Coke".
The "Count" member of the list variable returns how many elements the list has (here: 3). The iterating variable i starts with 1 continues to 2 and is lastly 3. So in the first cycle the Label with ID 1 is assigned with "list[1-1]" which is list[0] which is the first element of the list because indexing starts with 0.

In all examples so far, "i" started with 1 and is increased by 1 in each cycle until it reached the end value. It is increased by "1" because that is the default value for the step count. If you like to increment differently, simply define it via: Step Value
The following script has the cycles: 1,3,5,7,9.

For i = 1 to 10 Step 2 {
 Label1.Text = "Current i is: " + i
 WDWait(1)
}

Other examples which combine the for-loop with an if-statement and Project members can be found in the chapter "Project and Context Member".

 

Exiting the For Loop

With the statement "break" it is possible to exit a for loop before it finishes all its cycles, which is especially of interest the longer and complex your for loops get.
The usual case is to define a break condition with an if-statement. As soon as the "break" expression is called, everything following this expression is skipped and the script resumes after the brackets indicating the end of the for loop.

For i = 1 to 10 {

 if varLimit = 42 {
         break
 }
 Label1.Text=i
 WDWait(1)
}

Label1.Text = "end"

In this example, the for loop is only executed as long as the if-statement is not true. As soon as the variable "varLimit" equals 42, the for loop is canceled, i.e. no more cycles are executed. The script continues immediately with the line that follows the for loop which is in this example the one telling the Label to display "end".

In short: Everything following "break" within the for loop will not be executed!

 

You might think of another solution with a nested if but without a break statement. Based on the above example, the following could also be an option if you like to run the for loop only under the circumstance that varLimit does not equal 42 (the syntax for "equals not" is: !=).

For i = 1 to 10 {
 if varLimit != 42 {
         Label1.Text=i
         WDWait(1)

 }
}

Label1.Text = "end"

In difference to the first example, here, ALL cycles are initialized which means that the script checks varLimit 10 times. Depending on the true/false answer, the Label displays a new number or does not, but the for loop itself continues running and is not canceled as above. With a short example like this, there is not a big difference which work flow you attempt. However, the longer or complex the for loops get, the more it is of interest to use a solution with a better performance which means canceling for loops when they are not needed any more by using the break statement.

The following script is a good example for that. Imagine you have a ListView with 1000 rows showing a name in one cell and associated data in other cells. A Textbox allows to enter a name and with the following "Search" script you could highlight the row that contains that name and then quit the search process.

var SearchWord = TextBox1.Text
for i=1 to 1000 {
 if ListView1.GetCell(1,i) = SearchWord {
         ListView1.SelectedRow = i
         break
 }
}

 

The next topic explains the "exit" statement which provides the same functionality, but stops the complete script following the statement and does not resume after the for loop.