Looping Statements

Looping statements allow you to have the script perform a task or set of tasks repeatedly until the specified ending condition is met. This is helpful if you have a series of tasks you want to perform on each member of a set. 

The keywords for looping statements are: 

Looping statements always require conditional code blocks. Blocks are enclosed in curly braces ( { } ). You can include the following optional statement in the block: 

BREAK: Terminates a looping set of statements.

FOR Statements

Description: Repeats a statement or series of statements a specific number of times.

Syntax: FOR <var> = <start> TO <end> <{ ... [BREAK] }>

Details: FOR loop repeats the code in the curly braces {...} once for each whole number value in the range of <start> through <end>. The <start> and <end> clauses can be expressions or literal values. With each iteration, the current value in the range is assigned to a variable <var> named in the header line of the loop. This is called the iterator variable.

The optional BREAK clause aborts the loop. The script then continues, executing the code after the closing curly brace or moving on to the next Studio actionClosed Performs a process within a Studio script, such as collecting customer data, playing a message or music, or routing a contact to an agent..

The initial value of <start> and <end> are evaluated once, at the beginning of the loop. You can modify the <end> value in the code statements in the curly braces {...}. You can also modify the value of <var> from within the loop.

The value of <start> doesn't need to be less than <end>. If <end> is smaller than <start>, the loop traverses downward. At least one iteration of the loop will always occur. This behavior is different from some other programming languages. The following examples illustrate this behavior.

In this example, <end> is larger than <start>. The result is ph="123456789".

FOR i=1 TO 9
{
	ph = "{ph}{i}"
}

In this example, <start> is larger than <end>. The result is ph="987654321".

FOR i=9 TO 1
{
	ph = "{ph}{i}"
}

Examples

IF TEST = 1
{
  ASSIGN Names = "Wes|Nate|Dan|Clay"
  ASSIGN EyeColors = "Blue|Blue|Brown|Brown"
  ASSIGN Signs = "Aquarius|Aries|Pisces|Leo"
  ASSIGN Foods = "Spagetti|Pizza|Sushi|Deep Fried Twinkies"
}

DYNAMIC Persons

ASSIGN PeopleCount = Names.size

FOR i = 1 TO PeopleCount
{
  ASSIGN Persons[i].Name = "{Names[i]}"
  ASSIGN Persons[i].Eyecolor = "{EyeColors[i]}"
  ASSIGN Persons[i].Sign = "{Signs[i]}"
  ASSIGN Persons[i].Food = "{Foods[i]}"
}
FOR i=1 TO 9
{
	a = "{a}{i}"
}
//Result: a="123456789"


FOR i=9 TO 1
{
	b = "{b}{i}"
}
//Result: b="987654321"

FOREACH Statements

Description: Repeats a loop once for each element in the specified string array.

Format: FOREACH <var> IN <array> <{ ... [ BREAK ] }>

Details: A FOREACH loop repeats the code within the curly braces {...} once for each element of a string array or dynamic data object array. The value of each array element is assigned to a variable <var> named in the header line of the loop.

The optional BREAK clause aborts the loop. The script then continues, executing the code after the closing curly brace or moving on to the next Studio actionClosed Performs a process within a Studio script, such as collecting customer data, playing a message or music, or routing a contact to an agent..

The following example shows a FOREACH loop that includes an IF statement.



IF TEST = 1 
{
	ASSIGN Names="Odin|Freya|Thor|Loki"
}
ASSIGN SubCounter=1
FOREACH Name IN Names
{
	IF Name = "Odin" |  Name = "Freya"
	{
		ASSIGN NewNames[SubCounter]= "{Name}"

		ASSIGN SubCounter=SubCounter + 1

	}
}

REPEAT Statements

Description: Performs the designated commands a specified number of times.

Syntax: REPEAT <expression> <{ ... [ BREAK ]}>

Details:  This command repeats the code that's contained in the curly braces {...} a number of times defined by <expression>. The <expression> can be any literal positive integer or an expression that evaluates to zero or a positive integer.

Include the optional BREAK keyword to abort the loop. The script then continues, executing the code after the closing curly bracket of the REPEAT block or continues on to the next Studio actionClosed Performs a process within a Studio script, such as collecting customer data, playing a message or music, or routing a contact to an agent..

Tree View Properties

On the Tree View tab, REPEAT appears as REPEAT 0.

It has the following properties to configure:

  • Comment: Add a comment about the repeat.
  • Line Number: The line number the command is on in the editor.
  • Repeat: Enter an expression that evaluates to a numeric value to define the number of times you want the specified behaviors to repeat.

To complete REPEAT, right-click on REPEAT 0 and select the behavior you want the script to repeat. You can add more than one behavior. Configure each selected behavior or behaviors.

Example

To see this example in Tree View, copy and paste into the Text View tab, then click the Tree View tab.


REPEAT 10
{
	phone = "{phone}{random(10)}"
}	

BREAK Statements

Description: Immediately ends a loop.

Syntax: BREAK

Details: When the BREAK statement is reached, the current loop ends immediately. The script continues executing the snippet code below the loop until complete.

BREAK is optional. If it's not included, the loop continues until the defined stop condition is met.