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 will look like in a WD script:

Basic syntax

For i = Value1 to Value2 Step Value{

 Script

}

 

It does not matter whether the curly brackets are in the same or next line, but it is strongly 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 */

 

Script Example

For i = 1 to 10 {

 

 WDFaderUp(1,2)

 WDWait(2)

 WDFaderDown(1,2)

}

 

This script will execute 10 times the code between the curly brackets.
A special local variable is defined and iterated by the step count for every further loop. In this case, "i" is set to "1" and will be increased by one (if you leave the optional step count, 1 is the default value) in each loop until it reaches "10". Hence you can also write:

For i = 1 to 10 Step 1{

 

 WDFaderUp(1,2)

 WDWait(2)

 WDFaderDown(1,2)

}

 

You can make use of your iteration variable by putting it e.g. at your commands. The following script fades the faders with ID 2,4,6,8 and 10 each up and down within four seconds.

For i = 1 to 10 Step 2{

 

 WDFaderUp(i+1,2)

 WDWait(2)

 WDFaderDown(i+1,2)

}

 

You can use any valid name for the iteration variable, it doesn't necessarily have to be "i".

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

 

Please do not use already defined variables as iteration variable, too.

 

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 finished all its actual loops. 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.

Example:

For i = 1 to 10 {

 

 WDFaderUp(1,2)

 WDWait(2)

 WDFaderDown(1,2)

 

 if i = 5 {

         break

 }

}

In this example, the loop is only executed five times instead of ten.

Please be aware that everything following the "brake" statement within the for loop will not be executed!