Loops – Codeblocks
Understanding loops.
Loop code blocks control how many times the block body is run.
These are called loops since the body is repeated multiple times, reminding us of loops of rope.
A pass through the loop is called an iteration.
The simplest repeat block runs the code in its body, the specified number of times.
For example, here the block will print Informed Design three times.
Additionally, you can add a math block to calculate the number of times to iterate through the loop.
Here, a parameter is used to calculate a value by subtracting four from the wall height value.
When the loop is run, the block will print Informed Design four times calculated from the wall height value of eight minus four.
Updating the wall height prompts the loop block to run and recalculate the number of loops to print Informed Design.
The repeat while loop block will iterate over the block as long as the condition is true.
Here, a variable called I is initially set to zero, and the loop is evaluated to run while I is less than the wall height.
The due portion of the block is set to print the value of I when the code block is run.
This is done by using the print block combined with a create text with block. And then adding a text block combined with another block to get the value of I.
When the form is used to test the code block, an infinite loop error is displayed informing us that the while condition never terminated to end the loop. To resolve the error, a change by block is added to increment the I variable by a value of one each time the loop is repeated.
Using the form to test the code again, Now demonstrates that the loop is advancing the value of I by one for each iteration, but only while I is less than the parameter value.
You can use the drop down list to convert a repeat while loop to a repeat until loop.
Repeat until loops are similar to while loops, but they only repeat until the condition is true and then they stop looping.
The count with block iterates a variable from one value to another value by a specified increment amount. Looping once for each step.
Here, the block prints the numbers between one and twelve, incrementing by three. In this example, the increment is set to three. So I advances from ten to thirteen, and therefore never reaches twelve. So ten is the last number printed.
The four each block iterates over the values in a list. So rather than stating loop x many times, the four each block states loop through every item in the set of values.
In this example, each value is displayed in the print pop up window.
Here, a four each block is placed in the canvas.
Then a variable called material is created, and the for each block is set to use this variable as the iterator.
Next, the parameter is selected to use as the list to iterate or loop over.
Attaching the block straight from the parameters library is not allowed because the foreach block expects a list and not just a value.
To resolve this, the parameter function is changed from the singular value to the plural.
Values, which is a list.
To display the list of values, the print block is added from the text code blocks library, and then the material variable is added to it.
To run the code and generate the print pop up containing the list of materials, The update button is pressed.
Most loops run until a terminating condition is reached, or until all values have been looped through. But in some circumstances, adding a loop termination block is desirable.
Here, a block is added to exit or break out of the loop if the list encounters high impact gypsum.
The print pop up displays every item in the list that was encountered up until high impact was reached, and the loop was exited.
The termination block can be changed from breakout to continue with next iteration in order to simply skip printing high impact gypsum.
In summary, loop blocks provide a way for your logic to efficiently run multiple times.
Repeat loops allow a block to loop while or until a condition is met.
The count with block loops from one number to another by a specified side increment.
The for each loop iterates through values in a list.
Loop termination blocks exit the block or skip an iteration when a condition is met.