forLoopAdded by IBM on October 10, 2012 | Version 1 (Original)
|Creates a loop that you can use to run a compute a number of times.
The forLoop function uses an option in your form, such as a custom option, as an index that stores the current count of the loop. As the loop counts, it sets this option to the current count. For example, if the for loop counted from 1 to 3, it would first set the option to 1, then to 2, then to 3. Each time the loop increments, it will perform a particular compute.
A form with a for loop begins counting that loop as soon as the form opens, unless the loop itself relies on a triggering event, such as a keypress event or atoggle function.
Creating a Loop that Counts Once You can create a loop that counts once by setting the initial count and the final count to be equal. For example, a loop that counts from 1 to 1 will count once.
Table 1. forLoop parameters
forLoop(indexReference, initialIndex, maxIndex, compute)
|indexReference||Reference string||A reference to the option that stores the current count of the for loop. If this option does not exist, the function creates it with an empty value.|
|initialIndex||Integer||The starting value of the for loop. This value is inclusive, meaning that the for loop begins by counting this value.|
|maxIndex||Integer||The ending value of the for loop. This value in inclusive, meaning that the for loop ends by counting this value.|
|compute||String||The compute that you want to run each time the loop counts.|
Returns 1 on success or 0 (zero) on failure.
Example In this example, the custom:toggle1 option contains a compute that is triggered when a button in the form is clicked. After it is triggered, the compute does three things:
- Resets the result field to a value of 1.
- Starts a for loop that runs from 1 to x, where x is a number typed in by the user.
- Runs a compute each time the count changes. This compute multiplies the value of the result field by the current count, then sets that value to the result field.
When run, this compute creates a loop that calculates the factorial of the number. On the first count, 1 * 1 is calculated and stored in the result field. On the second count, the value of the result field (1) * 2 is calculated and stored in the result field. On the third count, the value of the result field (2) * 3 is calculated, and stored in the result field. These calculations continue until the final value has been calculated and stored in the result field.
<value>Click to calculate</value>
calculateButton.activated, 'off', 'on') == '1' ?
set('resultField.value', '1') +
forLoop('custom:counter', '1', numberField.value,
set('resultField.value', resultField.value *
custom:counter)) : ''"></custom:toggle1>
- You can create a loop that counts once by setting the initial count and the final count to be equal. For example, a loop that counts from 1 to 1 will count once.