Codeblocks - use Loop blocks
Use Loop blocks to iterate, or loop, through logic statements in the Informed Design Codeblocks Rule Editor.
Step-by-step guide
Loop code blocks provide a way to control how many times a block body is run. Loops indicate that the body of the code block is repeated, or looped through, multiple times. An iteration is a pass through a single loop.
Repeat blocks
The simplest Repeat block runs the code in its body the specified number of times.
- From the Loops blocks, add a Repeat block to the canvas.
- From the Text blocks, add a Print block and a Text box to the Do portion of the statement.
- Enter the desired text and update the repeat value to 3.
- In the form, click Update form.
The specified text is printed three times.
Use a Math block in conjunction with a Repeat block to calculate the number of iterations through a loop.
- Start with a new Repeat block, add the previous Print block, and from the Math library, add a Calculation block to the Repeat portion of the statement.
- Add a Nominal Wall Height value parameter and a Math Number block to the calculation.
- Set the calculation drop-down to minus (-) and the Math Number value to 4.
- Click Update form to run the loop.
The block calculates the number of times to print by subtracting 4 from the Nominal Wall Height value. In this case, the text is printed 4 times.
- In the form, update the Nominal Wall Height to 10.
The Loop block automatically runs, recalculates the number of loops, and prints the text 6 times.
Repeat While and Repeat Until blocks
Use the Repeat While Loop block to iterate over a code block as long as a specific condition is true.
- From the Loops library, add the Repeat While block.
- Add a Set To variable block above the Repeat While block and set the variable drop-down to i.
- Add a Math Number block after the variable block and set it to 0.
- From the Logic library, add a Calculation block to the Repeat While portion of the statement.
- Add an i variable, the Nominal Wall Height parameter, and the less than (<) operator, so that the loop is evaluated to run while i is less than the wall height.
- From the Text blocks, add Print and Create Text With blocks to the Do portion of the statement.
- Add a Text block to the upper portion of the Create Text With block, and enter the text “i=”.
- Add an i variable block below the Text block to finish the Do statement.
- In the form, change the Nominal Wall Height to another value, such as 3, to test the code block.
An infinite loop error displays, because the While condition never terminated to end the loop.
To resolve the error:
- Add a Change By variable block to the Do statement and set the drop-down to i. This increments the i variable by 1 each time the loop repeats.
- Adjust the Nominal Wall Height again to test the code block.
The loop advances the value of i by 1 for each iteration, but only while i is less than the parameter value. For example, with a Nominal Wall Height value of 4, the value of i is printed from 0 to 3.
Repeat Until loops are like While loops, but they repeat until the condition is true, and then they stop looping.
- In the Repeat While block, change the Repeat drop-down to until to convert it to a Repeat Until loop.
- Change the Calculation to equals (=) and move the Change By block above the Print block.
- Update the Nominal Wall Height to 5 to test the code block.
The code block runs until the i variable equals the value of the Nominal Wall Height parameter.
Count With blocks
The Count With block iterates a variable from one value to another value by a specified increment amount, looping once for each step.
- Add the Count With block to the canvas, set the variable to i, and add Math Number blocks, so that the block prints the numbers between 1 and 12, incrementing by 3.
- Add Print and i variable blocks to the Do portion of the statement.
- Click Update form to run the code block.
Because the increment is set to 3, the next increment after 10 is 13, so 10 is the last number printed.
For Each blocks
The For Each block iterates over the values in a list. Rather than stating loop x many times, the For Each block states loop through every item in a set of values.
- When the example shown below is run, each value displays in the popup window.
- Using an already created Interior Panel Material list, add a For Each block to the canvas.
- In the Variables library, click Create variable, name it “material”, then click OK.
- Set the For Each block drop-down to use material as the iterator.
- Select the Interior Panel Material parameter to use as the list to iterate or loop over.
Note that attaching the block straight from the Parameters library is not allowed, because the For Each block expects a list and not a single value.
- To resolve this, in the drop-down, change the Parameter function to the plural, values, which denotes a list.
- In the Do portion of the statement, add a Print text block and a Material variable.
- Click Update form to run the code and generate the print popup, which contains all three materials in the list.
Loop Termination blocks
Most loops run until a terminating condition is reached, or until all values have been looped through. Use a Loop Termination block to exit, or break, from a loop.
In this example, the Loop Termination block is placed in the Do portion of the For Each If Do statement and is set to break out of the loop if the list encounters “High Impact Gypsum”.
The Loop Termination block can be set to skip printing the specified material.
- Expand the Loop Termination drop-down and select continue with next iteration.
Now, running the For Each block prints all the materials in the list except High Impact Gypsum.
In summary, loop blocks provide a way for logic to efficiently run multiple times. Repeat loops allow a block to loop while, or until, a condition is met. Count With blocks loop from one number to another by a specified increment. For Each loops iterate through values in a list. Loop Termination blocks exit a block or skip an iteration when a condition is met.