Loops
This article will teach you about the Loops blocks in our Dynamic Engine.
Last updated
This article will teach you about the Loops blocks in our Dynamic Engine.
Last updated
A loop is a sequence of blocks which is specified once but which may be executed several times in succession. These structures are called loops since the blocks inside the loop (the body of the loop) is repeated multiple times, reminiscent of a rope containing loops. Each pass through the loop is called an iteration.
For more information on Loops, read this Wikipedia page.
There are four kinds of loops blocks you can use in the Dynamic Engine, as well as a special loop termination block that allows you to terminate (aka short circuit) a loop if a certain condition is met. The four loops blocks are:
Repeat N Times
Repeat While/Until
Count With
For Each in List
The loop termination block is: Break Out or Continue With Next Iteration
The simplest repeat block runs the code in its body the specified number of times. For example, the following block will log "Hello!" ten times.
The options for the repeat dropdown do the following:
While - repeats while the condition is true
Until - repeats until the condition is true
The Repeat While block repeats its body while some condition is true. To determine whether a condition is true or false, Logic Blocks need to be used. In the following example, the Repeat While block is used to assign 3 random numbers between 1 and 100 to a variable named Sorted List, but continue doing that while those random numbers are not sorted in ascending order.
In the example above, it's uncertain how many times the body will be executed. It may execute once or hundreds of times, depending on when the while condition becomes false.
For more information on Logic Blocks, see the Logic Blocks Page.
The Repeat Until block is very similar to the Repeat While block, except the condition is reversed. Repeat While loops repeat their bodies while some condition is true, while Repeat Until loops repeat their bodies until some condition is true. The following blocks are equivalent to the previous example because the loop continues until the sorted list variable is sorted in ascending order.
The Count With block advances a variable from the first value to the second value by the increment amount (third value), running the body once for each value. For example, the following Count With block starts by assigning the value 1 to the variable i , then 2, then 3, and so on until it reaches 10. This block will log values of i from 1 to 10 in ascending order.
Note that Count With is called a For Loop in most programming languages
The For Each in List block is similar to the Count With block, except instead of giving the loop variable values in a numeric sequence, it uses the values from a list in turn. The following For Each block logs each color in the colors list: "red", "green", "blue".
For more information on For Each, read this Wikipedia Page
The Break Out or Continue With Next Iteration blocks differ from other loops blocks, as they are loop termination blocks. They are meant to be used in conjunction with the other loops blocks.
The Break Out block provides an early exit from a loop. The following example logs "red" on the first iteration and breaks out of the loop on the second iteration when the loop variable is equal to "green". The third item in the list is never reached.
The Continue With Next Iteration block causes the remaining code in the body to be skipped and for the next iteration of the loop to begin. The following example logs "red" on the first iteration of the loop. On the second iteration, the Continue With Next Iteration block is run, skipping the logging of "green". On the final iteration, "blue" is logged.
Note that Continue With Next Iteration is called continue in most programming languages
Consider the following Fill-in-the-blank question in EXAMIND:
The goal of this question is to create a simple question that doesn't have negative net income. Using a loop will ensure that the random numbers will only generate a positive net income. Here is what the Dynamic Engine would look like:
Loops are very handy when you want to add some control to the randomized scenarios. In this case, no student will ever receive a question where net income is not above $0.
Here is what a sample question would look like (values highlighted in blue for convenience):