Open topic with navigation

TIBCO Business Studio™ - BPM Edition 5.4.0

# 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 Cloud BPM 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 - Cloud 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;
    }

Did you find this helpful?

Yes No
Great!
Thanks for taking the time to give us some feedback.
Sorry about that

Why wasn't this helpful? (check all that apply)

Could not find what I was looking for
Instructions confusing or unclear
Instructions did not work

Want to tell us more?

Please select at least one option or provide details about the issue!!

Error occurred while submitting feedback.

Error2 occurred while submitting feedback.

TIBCO Business Studio™ - BPM Edition

Copyright © 2015-2023. Cloud Software Group, Inc. All Rights Reserved.