• Informed Design
  • Inventor

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.

  1. From the Loops blocks, add a Repeat block to the canvas.
  • In the Informed Design interface, Rules workspace, the Logic blocks library expanded and a Repeat block being added to the canvas.
  1. From the Text blocks, add a Print block and a Text box to the Do portion of the statement.
  2. Enter the desired text and update the repeat value to 3.
  3. In the form, click Update form.

The specified text is printed three times.

  • A code block with Repeat, Print, and Text blocks set to print the specified text three times; and the print popup, with the specified text printed three times.

Use a Math block in conjunction with a Repeat block to calculate the number of iterations through a loop.

  1. 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.
  2. Add a Nominal Wall Height value parameter and a Math Number block to the calculation.
  3. Set the calculation drop-down to minus (-) and the Math Number value to 4.
  4. Click Update form to run the loop.
  • On the canvas, a Repeat block with a Calculation block set to calculate the number of times to repeat the loop and print the specified text; and in the form, Update form being selected.

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.

  1. 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.

  1. From the Loops library, add the Repeat While block.
  2. Add a Set To variable block above the Repeat While block and set the variable drop-down to i.
  • The Set To variable block with the variable selection options shown and “i” being selected.
  1. Add a Math Number block after the variable block and set it to 0.
  2. From the Logic library, add a Calculation block to the Repeat While portion of the statement.
  3. 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.
  4. From the Text blocks, add Print and Create Text With blocks to the Do portion of the statement.
  5. Add a Text block to the upper portion of the Create Text With block, and enter the text “i=”.
  6. Add an i variable block below the Text block to finish the Do statement.
  • A Repeat While block with an added Set To variable block and the Repeat and Do portions configured for this example.
  1. 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.

  • The Infinite loop error message.

To resolve the error:

  1. 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.
  • The Change By variable block added to the code block to increment the “i” variable by 1 each time the loop repeats.
  1. 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.

  • The value of i 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.

  1. In the Repeat While block, change the Repeat drop-down to until to convert it to a Repeat Until loop.
  2. Change the Calculation to equals (=) and move the Change By block above the Print block.
  • The Repeat Until block set up so that the code block will run until i equals the Nominal Wall Height value.
  1. 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.

  1. 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.
  2. Add Print and i variable blocks to the Do portion of the statement.
  • The Count With block set to Print i from 1 to 12 in increments of 3.
  1. 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.

  • The values that print when the Count With block is set to Print i from 1 to 12 in increments of 3; the list includes 1, 4, 7, and 10.

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.

  1. When the example shown below is run, each value displays in the popup window.
  2. Using an already created Interior Panel Material list, add a For Each block to the canvas.
  3. In the Variables library, click Create variable, name it “material”, then click OK.
  4. Set the For Each block drop-down to use material as the iterator.
  5. 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.

  1. To resolve this, in the drop-down, change the Parameter function to the plural, values, which denotes a list.
  2. In the Do portion of the statement, add a Print text block and a Material variable.
  3. Click Update form to run the code and generate the print popup, which contains all three materials in the list.
  • The Interior Panel Material list; the For Each Block, with material set as the iterator and the block set to print the materials from the list; and the generated print popup, with all three materials printed.

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 added to the For Each block and set to Break Out of the loop when “High Impact Gypsum” is encountered.

The Loop Termination block can be set to skip printing the specified material.

  1. Expand the Loop Termination drop-down and select continue with next iteration.
  • The Loop Termination block with the drop-down expanded and continue with next iteration being selected.

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.