Loops Within Scripts

This section is intended chiefly for those readers not familiar with JavaScript.

There are three different JavaScript loop syntaxes that can be used in TIBCO BPM Enterprise Script, and one that cannot be used. The ones that are supported are:

while (CONDITION) { BLOCK }
do { BLOCK } while (CONDITION);
for (INITIALIZER; CONDITION; INCREMENT) { BLOCK }

One that is not supported is:

for (FIELD in ARRAYFIELD) { BLOCK }

Here is a simple while loop that loops until the ix variable becomes less than 0:

var result = "";
var ix = 10;
while (ix >= 0)
{
   result = result + " " + ix;
   ix = ix - 1;
}

Things to note:

  • result = result + " + ix; can be written result += " " + ix; using abbreviations allowed in JavaScript.
  • ix = ix - 1; can be written ix--; or --ix; using abbreviations allowed in JavaScript.
  • ix++; or ++ix; can be used similarly instead of ix = ix + 1; .
  • The curly braces are required for loops in TIBCO Business Studio - BPM Edition JavaScripts just as they are for if/else statements.

The do-while loop is very similar, but the condition is evaluated at the end of the loop, so the loop is always executed at least once:

var result = "";
var ix = 10;
do
{
   result += " " + ix;
   ix--;
}
while (ix >= 0);

The for loop is similar to the while loop, but has two extra expressions in it. One is executed before the loop starts and the other is executed at the end of each loop. It results in a more compact script. Here is the equivalent for loop to the above while loop:

var result = "";
for (var ix = 10; ix >= 0; ix--);
{
   result += " " + ix;
}