ForEach Loop

Navigation:  Widget Designer > Script Language > Programming Statements >

ForEach Loop

prev main next

Navigation:  Widget Designer > Script Language > Programming Statements >

ForEach Loop

prev main next

Loading...

If you want to loop through a list without using an index-variable (like For Loop does), then ForEach lets you do this efficiently. Generally, the body of the statement performs one or more operations for each item of the list being iterated over.

This is what it looks like in a script:

Basic Syntax

ForEach item in listVariable

{

DebugMessage(item)

}

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 tabs for a clear and easy-to-read arrangement.

Code blocks can be in/out-dented using [Tab] or [Shift + Tab].

To comment a line out without getting errors in the Debug Logger, use the following 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, for example if it takes too long. Further down, the "break" statement is explained.

Script Example

var myList = ["FooBar",1,2.0,true]

ForEach item in myList {

DebugMessage(item)

}

This script will iterate over myList executing the commands comprised in curly brackets. The local variable "item" holds each value during the current iteration. You can name it ("item") any way you want, just make sure not to use an already defined local or global variable for this matter.

The list ("myList") in this example comprises values of different types, just to show you can actually do that. DebugMessage() takes care of converting the values for you. Just be aware you might need to do conversions yourself while using different commands.

Exiting ForEach

As with For Loops, you can stop the iteration prematurely if you want without having to iterate over a list until the end. Use the "break" statement like in the example below:

var myList = ["FooBar",1,2.0,true]

ForEach item in myList{

 If item = 2.0 { break }

DebugMessage(item)

}

DebugMessage("Done iterating.")

In this example, the ForEach loop only runs until the condition is true. For the third value in the list (2.0), the If-Statement becomes true, executing the break that strops the iteration. The script continues immediately with the line following the ForEach loop, in this example the Debug output "Done iterating.".

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